ofstream

Using ofstream* wrapper class with overloaded << operator on endl

谁说我不能喝 提交于 2019-12-12 02:22:31
问题 C++ This is an attempt to make a class that mimics the output behavior of using the << operator of an ofstream , as far as being able to use std::endl and write string s is concerned. The class has a single data member, the ofstream pointer. The class has two overloaded << operators, one that takes an std::string and another that takes a pointer to a function, whose argument is an ostream reference and returns an ostream reference. That is the signature of std::endl , according to this.

created file in subdirectory but cannot open file

我的未来我决定 提交于 2019-12-11 19:47:47
问题 I followed the instructions given in this thread for creating a file in a sub-directory. ofstream forceFile; forceFile.open(".\\output_files\\error_log.csv", ios::out | ios::app); forceFile << "stuff" << "\r\n"; forceFile.close(); But now I have files, with size, in the base directory that cannot be opened and are named .\output_files\error_log.txt if I double click the file, I am told the file cannot be found and told to try a different path. if I open with notepad++ I am asked if I want to

ofstream::open creates file, but then crashes (bad pointer in locale::getloc()?)

大城市里の小女人 提交于 2019-12-11 11:52:09
问题 So I have some code that looks like this, written in and compiled with Visual Studio 2010: if ( outputFile.is_open() ) { outputFile.close(); } if ( !outputFile.is_open() ) // condition for sanity-checking { outputFile.open("errorOut.txt", ios::out); } This crashes on an access violation. Attaching a debugger shows that the first condition is false ( outputFile is not open), the second condition is true ( outputFile is closed, which is good since I just checked it). Then open() gets called,

ofstream leaking memory

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 11:14:16
问题 I have a C++ class that writes its data out to a binary std::ofstream . The class is storing the data as a boost:shared_array but I have eliminated this as the problem. The problem is with the call to write() on the ofstream . The issue is that it seems to leak memory. The system it is running on is CentOS 64bit, GCC 4.1.2. When looking at top and free when the application is running the executable itself does not continue to consume memory (as backed up by the memory profiler in Netbeans),

How do i create a file in a sub directory in C++?

元气小坏坏 提交于 2019-12-11 10:36:01
问题 Here is my code, how do i create a file in the sub directory contacts? Every time the file is created, it appears in the same directory as my program. int main(){ ofstream myfile("\\contacts"); myfile.open ("a"); myfile.close(); } 回答1: Specify the full path in the constructor: ofstream myfile(".\\contacts\\a"); // or just "contacts/a" if (myfile.is_open()) { } The posted code attempts to create a file called "\\contacts" and then another file called "a" . Note: that ofstream will not create

usage of ofstream

て烟熏妆下的殇ゞ 提交于 2019-12-11 04:52:33
问题 I'm kind of confused by ofstream. ofstream inherited from ostream. And it also inherited method "operator<<" from ostream. ofstream x; x << "hello world" << endl; //cout << "hello world" << endl; system("pause"); return 0; The above code clip is trying to use an object of ofsream to output "hello world" to the terminal just as cout did. The above code clip can compile but shows nothing. Why does it happen? Thanks, 回答1: It's been a long time, but IIRC of stream is an output_file-stream which

How do I add data at the end of a file in C++?

巧了我就是萌 提交于 2019-12-11 03:34:31
问题 I have followed instructions on the net and this code is suppose to add the inputs to the end of the file 'database'. But when i check, the data over rides the existing data. Please help, here is my code: int main(){ string name; string address; string handphone; cout << "Name: "; getline(cin, name); cout << "Address: "; getline(cin, address); cout << "Handphone Number: "; getline(cin, handphone); ofstream myfile("database", ios_base::ate); myfile.seekp( 0, ios_base::end ); myfile << name<

C++ ofstream vs. C++ cout piped to file

无人久伴 提交于 2019-12-11 01:51:37
问题 I'm writing a set of unit tests that write calculated values out to files. Each test produces a square matrix that holds anywhere from 50,000 to 500,000 doubles, and I have a total of 128 combinations of test cases. Is there any significant overhead involved in writing cout statements and then piping that output to files, or would I be better off writing directly to the file using an ofstream? 回答1: This is going to be dependent on your system and environment. This likely to be very little

C++ Writing to file and Console Output at the same time with simple code

若如初见. 提交于 2019-12-11 01:09:23
问题 I am trying to write the following entire function to a text file while still maintaining its console output functionality without having code redundancy. Is there a simple way to post an entire method's result to a file and console at the same time? #include<iostream> #include<fstream> void sports(){ cout<<"\nGame_Code\t\tGane\t\tCost\t\tStart Time\n"; cout<<"\nSP-9651\t\t Game 1 \t\t60\t\t08:00"; cout<<"\nSP-9652\t\t Game 2 \t\t60\t\t09:15"; cout<<"\nSP-9653\t\t Game 3 \t\t55\t\t09:55";

How to close ofstream after assigning to ostream?

对着背影说爱祢 提交于 2019-12-10 18:07:40
问题 I can do std::ostream& out = condition ? std::cout : std::ofstream(filename); but how do I close in case of out = std::ofstream(filename) ? 回答1: As I understood you want to close file stream using out ? You don't need to close it explicitly. std::fstream is RAII object, so it will close an opened file automatically at the end of enclosing scope. And of course, you can always cast out if you really need to close the file just now: if( ptr = dynamic_cast<std::ofstream*>(out) ) { ptr->close(); }