ofstream

Difference between casting ifstream to bool and using ifstream::is_open()

徘徊边缘 提交于 2019-12-04 07:30:29
Maybe a dummy question, but I need a clear answer to it. Is there any difference at all in the return of any of those functions int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return !!file; } int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return file.is_open(); } So in other words, my question is: does casting the fstream to bool give exactly the same result as fstream::is_open()? No. is_open checks only whether there is an associated file, while a cast to bool also checks whether the file is ready for I/O operations (e.g. the

ofstream exception handling

不打扰是莪最后的温柔 提交于 2019-12-04 03:28:17
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 generated. So what exception should I have written ?? Streams don't throw exceptions by default, but

Returning ifstream in a function

你说的曾经没有我的故事 提交于 2019-12-04 02:41:34
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: ifstream getFile() { string fileName; cout << "Please enter in the name of the file you'd like to open:

Does ofstream close its files automatically? [duplicate]

让人想犯罪 __ 提交于 2019-12-04 01:48:45
This question already has answers here : Closed 7 years ago . Possible Duplicate: do I need to close a std::fstream? int main() { ofstream a("a.txt"); a << "A" << endl; //a.close(); } This works fine, but isn't it necessary to close the file at the end of the program? ofstream will close files when its destructor is called, i.e. when it goes out of scope. However, calling close() certainly doesn't do any harm and expresses your intentions to maintenance programmers. Calling close() also allows you to check if the close() was successful because you can then also check the failbit : http://www

c++ - fstream and ofstream

守給你的承諾、 提交于 2019-12-04 00:23:12
问题 What is the difference between: fstream texfile; textfile.open("Test.txt"); and ofstream textfile; textfile.open("Test.txt"); Are their function the same? 回答1: ofstream only has methods for outputting, so for instance if you tried textfile >> whatever it would not compile. fstream can be used for input and output, although what will work depends on the flags you pass to the constructor / open . std::string s; std::ofstream ostream("file"); std::fstream stream("file", stream.out); ostream >> s

How does one write the hex values of a char in ASCII to a text file?

你离开我真会死。 提交于 2019-12-03 12:30:34
问题 Here is what I currently have so far: void WriteHexToFile( std::ofstream &stream, void *ptr, int buflen, char *prefix ) { unsigned char *buf = (unsigned char*)ptr; for( int i = 0; i < buflen; ++i ) { if( i % 16 == 0 ) { stream << prefix; } stream << buf[i] << ' '; } } I've tried doing stream.hex, stream.setf( std::ios::hex ), as well as searching Google for a bit. I've also tried: stream << stream.hex << (int)buf[i] << ' '; But that doesn't seem to work either. Here is an example of some

How to do fsync on an ofstream?

萝らか妹 提交于 2019-12-03 06:55:45
问题 I want to make sure that an ofstream has been written to the disk device. What's the portable way (portable on POSIX systems) of doing this? Does that solve the problem if I open the file separately in read-only append mode to get a file descriptor and call fsync with it? Like this: ofstream out(filename); /* ... write content into out ... */ out.close(); int fd = open(filename, O_APPEND); fsync(fd); close(fd); 回答1: If you're able to use Boost, try a file_descriptor_sink based stream, eg.:

Writing stringstream contents into ofstream

人盡茶涼 提交于 2019-12-03 05:23:52
问题 I'm currently using std::ofstream as follows: std::ofstream outFile; outFile.open(output_file); Then I attempt to pass a std::stringstream object to outFile as follows: GetHolesResults(..., std::ofstream &outFile){ float x = 1234; std::stringstream ss; ss << x << std::endl; outFile << ss; } Now my outFile contains nothing but garbage: "0012E708" repeated all over. In GetHolesResults I can write outFile << "Foo" << std:endl; and it will output correctly in outFile . Any suggestion on what I'm

How to do fsync on an ofstream?

妖精的绣舞 提交于 2019-12-02 20:39:50
I want to make sure that an ofstream has been written to the disk device. What's the portable way (portable on POSIX systems) of doing this? Does that solve the problem if I open the file separately in read-only append mode to get a file descriptor and call fsync with it? Like this: ofstream out(filename); /* ... write content into out ... */ out.close(); int fd = open(filename, O_APPEND); fsync(fd); close(fd); mrtimdog If you're able to use Boost, try a file_descriptor_sink based stream, eg.: boost::filesystem::path filePath("some-file"); boost::iostreams::stream<boost::iostreams::file

Writing stringstream contents into ofstream

最后都变了- 提交于 2019-12-02 19:52:37
I'm currently using std::ofstream as follows: std::ofstream outFile; outFile.open(output_file); Then I attempt to pass a std::stringstream object to outFile as follows: GetHolesResults(..., std::ofstream &outFile){ float x = 1234; std::stringstream ss; ss << x << std::endl; outFile << ss; } Now my outFile contains nothing but garbage: "0012E708" repeated all over. In GetHolesResults I can write outFile << "Foo" << std:endl; and it will output correctly in outFile . Any suggestion on what I'm doing wrong? You can do this, which doesn't need to create the string. It makes the output stream read