ofstream

Assigning cout to a variable name

↘锁芯ラ 提交于 2019-11-28 21:29:22
In ANSI C++, how can I assign the cout stream to a variable name? What I want to do is, if the user has specified an output file name, I send output there, otherwise, send it to the screen. So something like: ofstream outFile; if (outFileRequested) outFile.open("foo.txt", ios::out); else outFile = cout; // Will not compile because outFile does not have an // assignment operator outFile << "whatever" << endl; I tried doing this as a Macro function as well: #define OUTPUT outFileRequested?outFile:cout OUTPUT << "whatever" << endl; But that gave me a compiler error as well. I supposed I could

Is std::ofstream movable?

隐身守侯 提交于 2019-11-28 13:25:42
I have this map which compiles fine in MSVC10 : std::map<std::string, std::ofstream> m_logFiles; But on ubuntu using g++ 4.5 with C++0x enabled, I get the following error message : /usr/include/c++/4.5/bits/ios_base.h|785|error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private By using pointers instead of objects, I resolved the problem. Searching on the web, I learned that streams are not meant to be copied (the why was well explained). But my question is, is std::ofstream a movable type ? If it is, shouldn't it allow its use as a template parameter in the standard containers ? If

Why does ofstream insert a 0x0D byte before 0x0A?

拥有回忆 提交于 2019-11-28 11:18:22
I'm outputing an array of unsigned characters in C++ using ofstream fout("filename"); but it produces a spurious character in between. This is the part of the code that makes the problem: for(int i = 0; i < 12; i++) fout << DChuffTable[i]; and this is the definition of the array: unsigned char DChuffTable[12] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B}; In the output file I get a spurious 0x0D between 0x09 and 0x0A . I checked the array in debugging mode right before it's going to get printed and it's not changed. Please tell me what you think of this problem.

How to easily indent output to ofstream?

烂漫一生 提交于 2019-11-28 07:33:29
Is there an easy way to indent the output going to an ofstream object? I have a C++ character array that is null terminate and includes newlines. I'd like to output this to the stream but indent each line with two spaces. Is there an easy way to do this with the stream manipulators like you can change the base for integer output with special directives to the stream or do I have to manually process the array and insert the extra spaces manually at each line break detected? Seems like the string::right() manipulator is close: http://www.cplusplus.com/reference/iostream/manipulators/right/

“ofstream” as function argument

北慕城南 提交于 2019-11-28 06:16:14
Is there a way to pass output stream as argument like void foo (std::ofstream dumFile) {} I tried that but it gave error : class "std::basic_ofstream<char, std::char_traits<char>>" has no suitable copy constructor Of course there is. Just use reference. Like that: void foo (std::ofstream& dumFile) {} Otherwise the copy constructor will be invoked, but there is no such defined for the class ofstream . Joel Falcou You have to pass a reference to the ostream object as it has no copy constructor: void foo (std::ostream& dumFile) {} If you are using a C++11 conformant compiler and standard library,

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

删除回忆录丶 提交于 2019-11-28 05:45:29
问题 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

Check if ostream object is cout or ofstream, c++

☆樱花仙子☆ 提交于 2019-11-28 00:03:26
Is there a way in C++ to check if an ostream object is cout or a ofstream object? Something like: ostream& output(ostream& out) { if (out == cout) return out; else { out << "something different because its not going to the console" << endl; return out; } } The reason I want to do this, is that I want to overload the << operator to do two different things depending on what type of stream it is used with. Is it possible to just overload the << operator twice each time with a different type of stream? Updated to reflect intention better. It's possible by checking the stream's 'identity': if (

std::ofstream, check if file exists before writing

会有一股神秘感。 提交于 2019-11-27 17:59:58
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. 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 check @return true if the file exists, else false */ bool fileExists(const std::string& filename) { struct

Assigning cout to a variable name

霸气de小男生 提交于 2019-11-27 13:55:08
问题 In ANSI C++, how can I assign the cout stream to a variable name? What I want to do is, if the user has specified an output file name, I send output there, otherwise, send it to the screen. So something like: ofstream outFile; if (outFileRequested) outFile.open("foo.txt", ios::out); else outFile = cout; // Will not compile because outFile does not have an // assignment operator outFile << "whatever" << endl; I tried doing this as a Macro function as well: #define OUTPUT outFileRequested

Is std::ofstream movable?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 07:39:36
问题 I have this map which compiles fine in MSVC10 : std::map<std::string, std::ofstream> m_logFiles; But on ubuntu using g++ 4.5 with C++0x enabled, I get the following error message : /usr/include/c++/4.5/bits/ios_base.h|785|error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private By using pointers instead of objects, I resolved the problem. Searching on the web, I learned that streams are not meant to be copied (the why was well explained). But my question is, is std::ofstream a