ofstream

Initialize ofstream in constructor class, only working with c++11

 ̄綄美尐妖づ 提交于 2019-12-02 18:01:24
问题 The following code compile flowlessly using g++ -I. -std=c++0x -Wall -g -Werror *.cpp -o main but without the -std=c++0x switch, it says main.cpp: In constructor ‘Out<T>::Out(const string&) [with T = double, std::string = std::basic_string<char>]’: main.cpp:274:42: instantiated from here main.cpp:34:113: erreur: no matching function for call to ‘std::basic_ofstream<char>::basic_ofstream(const string&, std::_Ios_Openmode)’ main.cpp:34:113: note: candidates are: /usr/include/c++/4.6/fstream:629

ofstream doesn't write buffer to file

旧时模样 提交于 2019-12-02 13:14:30
问题 I'm trying to write the contents of buf pointer to the file created by ofstream. For some reason the file is empty, however the contents of buf is never empty... What am I doing wrong? void DLog::Log(const char *fmt, ...) { va_list varptr; va_start(varptr, fmt); int n = ::_vscprintf(fmt, varptr); char *buf = new char[n + 1]; ::vsprintf(buf, fmt, varptr); va_end(varptr); if (!m_filename.empty()) { std::ofstream ofstr(m_filename.c_str(), ios::out); ofstr << *buf; // contents of *buf are NEVER

Ofstream creates but wont write to file

余生长醉 提交于 2019-12-02 13:05:26
I have wrote some code that does a basic fizzbuzz program to test my logging class, for some reason the data is dumped to the console fine and the file is created fine however the log file is empty whenever I open it. My main is here: int main() { logger* loggerObj = logger::createLogger("log.txt"); for (int i = 1; i <= 100; i++) { loggerObj->createLogEvent(i); if (i == 15) { loggerObj->writeLog(); } } and this is my class: int logger::m_instanceCount = 0; logger* logger::loggerObj = new logger; string logger::m_fileName = "log.txt"; logger::logger() { } logger::~logger() { } logger* logger:

Initialize ofstream in constructor class, only working with c++11

拟墨画扇 提交于 2019-12-02 09:23:15
The following code compile flowlessly using g++ -I. -std=c++0x -Wall -g -Werror *.cpp -o main but without the -std=c++0x switch, it says main.cpp: In constructor ‘Out<T>::Out(const string&) [with T = double, std::string = std::basic_string<char>]’: main.cpp:274:42: instantiated from here main.cpp:34:113: erreur: no matching function for call to ‘std::basic_ofstream<char>::basic_ofstream(const string&, std::_Ios_Openmode)’ main.cpp:34:113: note: candidates are: /usr/include/c++/4.6/fstream:629:7: note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::ios_base::openmode)

C++ ofstream delete and cleanup

删除回忆录丶 提交于 2019-12-02 08:44:48
问题 I am writing a C++ ofstream that sometimes must be cleaned up - the file I am writing to should be deleted and the class deleted and cleaned up. How? (Except closing it and deleting it by name). (At least the file should not exist with the intended location and filename with which it was opened - tempfile directory could be OK) 回答1: As far as I know, there is no other way. Close the file and use remove with its name. This is probably best handled by some sort of RAII class; I regularly use an

How can I determine the current size of the file opened by std::ofstream?

雨燕双飞 提交于 2019-12-01 17:36:05
I have a class that has a filestream of type ofstream . The constructor opens the file in append mode and all the messages always get written at the end of the file. I need to write into outputFile up to some fixed size say 1Mb, then I need to close, rename, and compress it, and then open a new file of the same name. This needs to be done when a certain size of file is reached. I tried using tellg() but after reading stuffs (and this ) on internet, I understood that this is not the right approach. As I'm new to C++, I'm trying to find out the most optimized and correct way to get the accurate

How to create ofstream file with name of variable?

天涯浪子 提交于 2019-12-01 14:52:35
char NAME[256]; cin.getline (NAME,256); ofstream fout("NAME.txt"); //NAME??????? What i need to do to create file with NAME name? You could try: #include <string> #include <iostream> #include <fstream> int main() { // use a dynamic sized buffer, like std::string std::string filename; std::getline(std::cin, filename); // open file, // and define the openmode to output and truncate file if it exists before std::ofstream fout(filename.c_str(), std::ios::out | std::ios::trunc); // try to write if (fout) fout << "Hello World!\n"; else std::cout << "failed to open file\n"; } Some useful references:

How to create ofstream file with name of variable?

℡╲_俬逩灬. 提交于 2019-12-01 13:34:03
问题 char NAME[256]; cin.getline (NAME,256); ofstream fout("NAME.txt"); //NAME??????? What i need to do to create file with NAME name? 回答1: You could try: #include <string> #include <iostream> #include <fstream> int main() { // use a dynamic sized buffer, like std::string std::string filename; std::getline(std::cin, filename); // open file, // and define the openmode to output and truncate file if it exists before std::ofstream fout(filename.c_str(), std::ios::out | std::ios::trunc); // try to

Reading/writing files to/from a struct/class

限于喜欢 提交于 2019-12-01 13:01:52
I'd like to read a file into a struct or class, but after some reading i've gathered that its not a good idea to do something like: int MyClass::loadFile( const char *filePath ) { ifstream file ( filePath, ios::in | ios::binary ); file.read ((char*)this, 18); file.close(); return 0; } I'm guessing if i want to write a file from a struct/class this isn't kosher either: void MyClass::writeFile( string fileName ) { ofstream file( fileName, ofstream::binary ); file.write((char*)this, 18); file.close(); } It sounds like the reason i don't want to do this is because even if the data members of my

Is the default mode of ofstream implementation defined?

北城以北 提交于 2019-12-01 11:37:28
问题 Given the following code: std::ofstream stream("somefile"); if (!stream) { return 1; } When invoking .write(....) and using stdc++ and libc++ the stream is in binary mode ( std::ios::binary ). However when using MSVC (2015/2017RC1) it seems to be in text mode or something weird, because the the resulting file is larger than what is actually written. But if i explicitly set the mode std::ios::binary MSVC behaves similarly to the std::ofstream implementations of other standard libraries