ofstream

Read/Write struct containing string property to binary file

核能气质少年 提交于 2019-12-20 06:46:14
问题 I am running in to an issue while reading/writing a struct having complex data specifically string. my struct looks like below. struct MyRecord { char name[80]; string location; // <<== note this double balance; unsigned long account_num; }; I use different functions for reading and writing. This is the code I am using to write the data to file struct MyRecord acc; strcpy(acc.name, "R"); acc.location = "newlocation ok"; // <<== note this acc.balance = 1.3; acc.account_num = 34; ofstream

overloading << operator for c++ stl containers

烈酒焚心 提交于 2019-12-20 03:47:08
问题 I wish I could just print contents of a set/vector/map by using cout << . It doesn't seem so difficult for the stl designers to implement : Assuming that << is defined for T, << for a container could just iterate through the elements and print them using ofstream << . Is there an easy way to print them that I dont know of? If not, Is there an easy solution? I have read at places that extending stl classes is a bad idea. Is that so, and why? how about defining an something like an overloaded

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

不羁的心 提交于 2019-12-19 19:48:42
问题 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

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

♀尐吖头ヾ 提交于 2019-12-19 19:48:41
问题 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

overloaded operator << on ofstream concatenation problems

孤者浪人 提交于 2019-12-18 16:50:59
问题 I have the following code: struct simple { simple (int a1, int a2) : member1(a1), member2(a2) {} int member1; int member2; }; std::ofstream &operator << (std::ofstream &f, const simple &obj) { f<<obj.member1<<", "<<obj.member2; return f; } int main(int argc, const char *argv[]) { std::ofstream f("streamout.txt"); simple s(7,5); f << s; //#1 This works f << "label: " << s; //#2 This fails return 0; } I'm trying to understand why #1 works, while there are problems when trying to use the

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

廉价感情. 提交于 2019-12-18 12:59:09
问题 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! 回答1: From the C++ runtime's point of view, it should have been written to disk. From an OS

std::ofstream, check if file exists before writing

六月ゝ 毕业季﹏ 提交于 2019-12-17 15:34:16
问题 I am implementing file saving functionality within a Qt application using C++. I am looking for a way to check to see if the selected file already exists before writing to it, so that I can prompt a warning to the user. I am using an std::ofstream and I am not looking for a Boost solution. 回答1: This is one of my favorite tuck-away functions I keep on hand for multiple uses. #include <sys/stat.h> // Function: fileExists /** Check if a file exists @param[in] filename - the name of the file to

Partially truncating a stream (fstream or ofstream) in C++

早过忘川 提交于 2019-12-17 14:53:08
问题 I am trying to partially truncate (or shorten) an existing file, using fstream. I have tried writing an EOF character, but this seems to do nothing. Any help would be appreciated... 回答1: I don't think you can. There are many functions for moving "up and down" the wrapper hierarchy for HANDLE<->int<->FILE * , at least on Windows, but there is no "proper" to extract the FILE * from an iostreams object (if indeed it is even implemented with one). You may find this question to be of assistance.

Partially truncating a stream (fstream or ofstream) in C++

吃可爱长大的小学妹 提交于 2019-12-17 14:53:03
问题 I am trying to partially truncate (or shorten) an existing file, using fstream. I have tried writing an EOF character, but this seems to do nothing. Any help would be appreciated... 回答1: I don't think you can. There are many functions for moving "up and down" the wrapper hierarchy for HANDLE<->int<->FILE * , at least on Windows, but there is no "proper" to extract the FILE * from an iostreams object (if indeed it is even implemented with one). You may find this question to be of assistance.

std::ofstream dont show error on permission denied C++

a 夏天 提交于 2019-12-14 02:43:58
问题 The following code when path = "c:\" doesn't write to file c:\err.txt because permission is denied. But it doesn't generate an error at the same time. Rather, it outputs "OK". How I can check whether the permissions would allow the write? #include <cstdlib> #include <iostream> #include <fstream> using namespace std; bool writeLineToErr(string path, string err_line){ std::ofstream outfile(path+"err.txt", std::ios_base::app); if(!outfile){ cout<<"Error 1 "+path+"err.txt"+" can't open file!";