ofstream

do I need to close a std::fstream? [duplicate]

故事扮演 提交于 2019-11-27 07:32:12
Possible Duplicate: Do I need to manually close a ifstream? Do I need to call fstream.close() or is fstream a proper RAII object that closes the stream on destruction? I have a local std::ofstream object inside a method. Can I assume that the file is always closed after exiting this method without calling close? I could not find documentation of the destructor. I think the previous answers are misleading. fstream is a proper RAII object, it does close automatically at the end of the scope, and there is absolutely no need whatsoever to call close manually when closing at the end of the scope is

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

半世苍凉 提交于 2019-11-27 06:03:12
问题 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

“ofstream” as function argument

£可爱£侵袭症+ 提交于 2019-11-27 05:39:09
问题 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 回答1: 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 . 回答2: You have to pass a reference to the ostream object as it has no copy constructor:

Error handling in std::ofstream while writing data

有些话、适合烂在心里 提交于 2019-11-27 03:24:41
问题 I have a small program where i initialize a string and write to a file stream: #include<iostream> #include<fstream> using namespace std; int main() { std::ofstream ofs(file.c_str()); string s="Hello how are you"; if(ofs) ofs<<s; if(!ofs) { cout<<"Writing to file failed"<<endl; } return 0; } My diskspace is very less, and the statement " ofs< " fails. So I know that this is an error logically. The statement "if(!ofs)" does not encounter the above issue, hence I am unable to know why it failed.

How to write to middle of a file in C++?

限于喜欢 提交于 2019-11-27 02:15:20
I think this should be quite simple, but my googling didn't help so far... I need to write to an existing file in C++, but not necessarily at the end of the file. I know that when I just want to append text to my file, I can pass the flag ios:app when calling open on my stream object. However, this only let's me write to the very end of the file, but not into its middle. I made a short program to illustrate the issue: #include <iostream> #include <fstream> using namespace std; int main () { string path = "../test.csv"; fstream file; file.open(path); // ios::in and ios::out by default const int

How to easily indent output to ofstream?

六月ゝ 毕业季﹏ 提交于 2019-11-27 01:51:28
问题 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

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

丶灬走出姿态 提交于 2019-11-26 21:37:30
问题 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

do I need to close a std::fstream? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-11-26 17:40:03
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Do I need to manually close a ifstream? Do I need to call fstream.close() or is fstream a proper RAII object that closes the stream on destruction? I have a local std::ofstream object inside a method. Can I assume that the file is always closed after exiting this method without calling close? I could not find documentation of the destructor. 回答1: I think the previous answers are misleading. fstream is a proper

How to write to middle of a file in C++?

岁酱吖の 提交于 2019-11-26 12:32:00
问题 I think this should be quite simple, but my googling didn\'t help so far... I need to write to an existing file in C++, but not necessarily at the end of the file. I know that when I just want to append text to my file, I can pass the flag ios:app when calling open on my stream object. However, this only let\'s me write to the very end of the file, but not into its middle. I made a short program to illustrate the issue: #include <iostream> #include <fstream> using namespace std; int main () {

Read file line by line using ifstream in C++

余生颓废 提交于 2019-11-25 23:55:46
问题 The contents of file.txt are: 5 3 6 4 7 1 10 5 11 6 12 3 12 4 Where 5 3 is a coordinate pair. How do I process this data line by line in C++? I am able to get the first line, but how do I get the next line of the file? ifstream myfile; myfile.open (\"text.txt\"); 回答1: First, make an ifstream : #include <fstream> std::ifstream infile("thefile.txt"); The two standard methods are: Assume that every line consists of two numbers and read token by token: int a, b; while (infile >> a >> b) { //