ofstream

why std::wofstream do not print all wstring into file?

送分小仙女□ 提交于 2019-12-06 00:16:30
I have a std::wstring whose size is 139,580,199 characters. For debugging I printed it into file with this code: std::wofstream f(L"C:\\some file.txt"); f << buffer; f.close(); After that noticed that the end of string is missing. The created file size is 109,592,584 bytes (and the "size on disk" is 109,596,672 bytes). Also checked if buffer contains null chars, did this: size_t pos = buffer.find(L'\0'); Expecting result to be std::wstring::npos but it is 18446744073709551615 , but my string doesn't have null char at the end so probably it's ok. Can somebody explain, why I have not all string

ofstream exception handling

假装没事ソ 提交于 2019-12-05 21:58:26
问题 Deliberately I'm having this method which writes into a file, so I tried to handle the exception of the possiblity that I'm writing into a closed file: void printMe(ofstream& file) { try { file << "\t"+m_Type+"\t"+m_Id";"+"\n"; } catch (std::exception &e) { cout << "exception !! " << endl ; } }; But apparently std::exception is not the appropriate exception for a closed file error because I deliberately tried to use this method on an already closed file but my "exception !! " comment was not

C++ find size of ofstream

浪子不回头ぞ 提交于 2019-12-05 20:02:29
I have code that currently does something like the following: ofstream fout; fout.open("file.txt"); fout<<"blah blah "<<100<<","<<3.14; //get ofstream length here fout<<"write more stuff"<<endl; Is there a convenient way to find out the length of that line that is written at the stage I specified above? (in real life, the int 100 and float 3.14 are not constant and can change). Is there a good way to do what I want? EDIT: by length, I mean something that can be used using fseek, e.g. fseek(pFile, -linelength, SEEK_END); You want tellp . This is available for output streams (e.g., ostream,

Returning ifstream in a function

风格不统一 提交于 2019-12-05 19:25:58
问题 Here's probably a very noobish question for you: How (if at all possible) can I return an ifstream from a function? Basically, I need to obtain the filename of a database from the user, and if the database with that filename does not exist, then I need to create that file for the user. I know how to do that, but only by asking the user to restart the program after creating the file. I wanted to avoid that inconvenience for the user if possible, but the function below does not compile in gcc:

fstream ifstream I don't understand how to load a data file into my program

倖福魔咒の 提交于 2019-12-05 17:21:55
My professor is very smart but expects complete noobs like me to just know how to program c++ . I don't understand how the fstream function works. I will have a data file with three columns of data. I will have to determine with a logarithm whether each line of data represents a circle, rectangle or triangle - that part is easy. The part I don't understand is how the fstream function works. I think I: #include < fstream > then I should declare my file object? ifstream Holes; then I open it: ifstream.open Holes; // ? I don't know what the proper syntax is and I can't find straightforward

Array of Ofstream in c++

[亡魂溺海] 提交于 2019-12-05 16:14:09
I want 41 output files to use in my project to write text on them. first create a string array list to name those output files then I tried to define an array of ofstream objects and use list to name them, but I get this error that 'outfile' cannot be used as a function . Below is my code: #include <sstream> #include <string> #include <iostream> #include <fstream> using namespace std ; int main () { string list [41]; int i=1; ofstream *outFile = new ofstream [41]; for (i=1;i<=41 ;i++) { stringstream sstm; sstm << "subnode" << i; list[i] = sstm.str(); } for (i=0;i<=41;i++) outFile[i] (list[i].c

C++, regarding fprintf and ofstream

社会主义新天地 提交于 2019-12-05 10:22:10
I've been using fprintf for a while now and I'd like to ask a question. What is the equivalent of this fprintf line: fprintf(OutputFile, "%s", "SomeStringValue"); using ofstream ? How to use the "%s" in ofstream is what I'd really like to know. How to take the next argument and print it as a string? You don't use it. The equivalent is essentially: std::ofstream x("your_file"); x << "SomeStringValue"; x.close(); Go read about it on any of several reference pages. Such as http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/ You can't. You just write the string to the stream. If you

Change or check the openmode of a std::ofstream

心已入冬 提交于 2019-12-05 06:40:14
In some code that does a lot of file i/o using std::ofstream , I'm caching the stream for efficiency. However, sometimes I need to change the openmode of the file (e.g. append vs truncate). Here is some similar mock code: class Logger { public: void write(const std::string& str, std::ios_base::openmode mode) { if (!myStream.is_open) myStream.open(path.c_str(), mode); /* Want: if (myStream.mode != mode) { myStream.close(); myStream.open(path.c_str(), mode); } */ myStream << str; } private: std::ofstream myStream; std::string path = "/foo/bar/baz"; } Does anyone know if: There is a way to change

How do I write binary data for 7z archive format?

ε祈祈猫儿з 提交于 2019-12-04 15:00:40
I've been pouring over the format description and source code for the 7z archive format, but I'm still having trouble writing a valid container. I assume I can create an empty container... anyway here's my start: std::ofstream ofs(archivename.c_str(), std::ios::binary|std::ios::trunc); Byte signature[6] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C}; Byte major = 0; Byte minor = 3; ofs.write((const char*)signature, 6); ofs.write((const char*)major, 1); ofs.write((const char*)minor, 1); UInt64 offset = 0; UInt64 size = 0; UInt32 crc = 0; ofs.write((const char*)offset, 4); ofs.write((const char*)size, 8);

C++ - How to have multiple threads write to a file

浪子不回头ぞ 提交于 2019-12-04 13:16:11
问题 I am currently writing a c++ program that uses threads to write strings to a file. I am using ofstream to write these strings, and I noticed that only one of the threads has access to the file. So my question: Is there any way to use ofstream in different threads to write to the same file? If it is possible, any examples would be great. If not, please let me know that as well, and some ways to get around this would be great. I have looked at the following link, except it didn't really make