ofstream

Reading/writing files to/from a struct/class

风格不统一 提交于 2019-12-01 11:15:24
问题 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

Is it allowed to write to a ofstream when it is not opened in c++

懵懂的女人 提交于 2019-12-01 09:29:09
I have a code like this: # in class definition std::ofstream m_myFile; ## some where in code m_myFile.open(filename); and then in several places, I am writing to file as follow: m_myFile << "some data to file"<<std::endl; This is working well, now I need to add a flag to system that when not set, this file should not be created and written to. I have checked and I can run the application if I do this: if(createFile) { m_myFile.open(filename); } and leave the write to file as it is and I am not getting any runtime error on windows. My question is if I am not opening a file and write to its

Read/Write to PPM Image File C++

不羁的心 提交于 2019-12-01 05:15:35
问题 Trying to read and write to/from a PPM Image file (.ppm) in the only way I know how: std::istream& operator >>(std::istream &inputStream, PPMObject &other) { inputStream.seekg(0, ios::end); int size = inputStream.tellg(); inputStream.seekg(0, ios::beg); other.m_Ptr = new char[size]; while (inputStream >> other.m_Ptr >> other.width >> other.height >> other.maxColVal) { other.magicNum = (string) other.m_Ptr; } return inputStream; } My values correspond to the actual file. So I cheerfully

c++ - fstream and ofstream

空扰寡人 提交于 2019-12-01 03:19:49
What is the difference between: fstream texfile; textfile.open("Test.txt"); and ofstream textfile; textfile.open("Test.txt"); Are their function the same? ofstream only has methods for outputting, so for instance if you tried textfile >> whatever it would not compile. fstream can be used for input and output, although what will work depends on the flags you pass to the constructor / open . std::string s; std::ofstream ostream("file"); std::fstream stream("file", stream.out); ostream >> s; // compiler error stream >> s; // no compiler error, but operation will fail. The comments have some more

ofstream doesn't flush

旧城冷巷雨未停 提交于 2019-12-01 01:07:53
问题 I have the following code, running on Suse 10.1 / G++ 4.1.0, and it doesn't write to the file: #include <fstream> #include <iostream> int main(){ std::ofstream file("file.out"); file << "Hello world"; } The file is correctly created and opened, but is empty. If I change the code to: #include <fstream> #include <iostream> int main(){ std::ofstream file("file.out"); file << "Hello world\n"; } (add a \n to the text), it works. I also tried flushing the ofstream, but it didn't work. Any

Writing char* to binary file using ostream::write

痞子三分冷 提交于 2019-11-30 21:12:37
问题 I am trying to write a char* to a binary file. This is what I have now. void Write(char* fileName, char* pData) { ofstream binFile (fileName, ios::out | ios::binary); if (binFile.open()) { binFile.write((char*)&pData, sizeof(pData)); binFile.close(); } } void Read(char* fileName, char* pData) { ifstream binFile(fileName, ios::in | ios::binary); if(binFile.open()) { binFile.read(char*)&pData, sizeof(pData)); binFile.close } } int main() { char* testData = "ABCdEFG"; // not real data char*

std::ofstream::write adds characters

◇◆丶佛笑我妖孽 提交于 2019-11-30 09:37:45
I am trying to write binary file using std::ofstream::write method. I found out, that some characters are not written as they are, for example: std::ofstream in("testout"); int i =10; in.write((const char *)(&i), sizeof(i)); in.close(); return 0; will write the following into a binary file: 0d 0a 00 00 00 Why is there additional 0d byte appearing? deviantfan You´ll have to specify std::ofstream::binary when opening. Else, on Windows in textfile mode, \n ( 0x0a ) in a program will be converted to/from \r\n ( 0x0d 0x0a ) when writing/reading the file. littleadv You opened the file in text mode

c++ std::ofstream flush() but not close()

ぐ巨炮叔叔 提交于 2019-11-30 08:40:06
I'm on MacOSX. In the logger part of my application, I'm dumping data to a file. suppose I have a globally declared std::ofstream outFile("log"); and in my logging code I have: outFile << "......." ; outFile.flush(); Now, suppose my code crashes after the flush() happens; Is the stuff written to outFile before the flush() guaranteed to be written to disk (note that I don't call a close() ). Thanks! From the C++ runtime's point of view, it should have been written to disk. From an OS perspective it might still linger in a buffer, but that's only going to be an issue if your whole machine

std::ofstream::write adds characters

你说的曾经没有我的故事 提交于 2019-11-29 14:14:02
问题 I am trying to write binary file using std::ofstream::write method. I found out, that some characters are not written as they are, for example: std::ofstream in("testout"); int i =10; in.write((const char *)(&i), sizeof(i)); in.close(); return 0; will write the following into a binary file: 0d 0a 00 00 00 Why is there additional 0d byte appearing? 回答1: You´ll have to specify std::ofstream::binary when opening. Else, on Windows in textfile mode, \n ( 0x0a ) in a program will be converted to

Writing Unicode to a file in C++

流过昼夜 提交于 2019-11-29 10:03:58
问题 I have a problem with writing unicode to a file in C++. I want to write to a file with my own extension a few smiley faces that you can get by typing ALT+NUMPAD(2). I can display it on CMD by making a char and assigning the value of '\2' to it and it will display a smiley face, but it won't write it to a file. Here is a snippet of code for my program: ofstream myfile; myfile.open("C:\Users\My Username\test.exampleCodeFile"); myfile << "\2"; myfile.close(); It will write to the file, but it