ofstream

Any reason why an std::ofstream object won't close properly?

自古美人都是妖i 提交于 2019-12-13 16:34:54
问题 I noticed in my C++ code that anytime I close an std::ofstream object I'm unable to reopen the file I closed with std::ifstream . std::ifstream 's open function will always fail. Is there anything 'extra' I can do to ensure that my std::ofstream object closes properly? Someone's probably going to ask to see my specific code so for the sake of keeping this post small I've pastied it here. In my code after running through case a or d all std::ifstream open calls fail. (Prior to posting this

File content truncated when using polish characters

≯℡__Kan透↙ 提交于 2019-12-13 15:25:24
问题 Code: #include <fstream> const wchar_t * testArray[] = { L"Wszystkie kategorie równoważne", L"Oczekiwane przepływy pieniężne", L"Risk i dojrzałość", L"Pozycja strategiczna i lata na rynku", L"Prawdopodobieństwo oszacowania" }; void FaultyFunction(void) { std::wofstream file("test.txt"); for (int i = 0 ; i < 100 ; ++i) { for (int j = 0 ; j < 5 ; ++j) { file << testArray[j] << L'\t'; } file << L'\n'; } } int main(void) { FaultyFunction(); return 0; } "test.txt" after execution: Wszystkie

How to search record using an id

两盒软妹~` 提交于 2019-12-13 08:35:46
问题 I am creating the system that outputs all the registered student in one of my textfile e.g 123 Michael 412 Voker 512 Karl 143 Riki I need to use their ID to search for the student Means i need to read this register student file. The system will only ask for the ID. e.g Type ID num: 123 OUTPUT: Hello Michael your ID number is 123. 回答1: Probably your are looking for a struct or class. Make a Student class I would say. class Student{ public: int id; char *name; }; Student s; Give a go through

Delete a line in an ofstream in C++

戏子无情 提交于 2019-12-13 03:48:38
问题 I want to erase lines within a file. I know you can store the content of the file (in a vector for example), erase the line and write again. However, it feels very cumbersome, and not very efficient if the file gets bigger. Anyone knows of a better, more efficient, more elegant way of doing it? 回答1: There's nothing particularly magical about disk files. They still like to store their data in contiguous areas (typically called something like "blocks"). They don't have ways of leaving data-free

C++ how to add/subtract tellp(), tellg() return

倖福魔咒の 提交于 2019-12-12 17:20:52
问题 Say I wanted to get the difference (in int) between two tellp() outputs. The tellp() output could be huge if a big file is written so it is not safe to say store it inside a long long. Is there a safe way to perform a operation like this: ofstream fout; fout.open("test.txt",ios::out | ios::app); int start = fout.tellp(); fout<<"blah blah "<<100<<","<<3.14; int end = fout.tellp(); int difference = end-start; Here, I know that the difference between end and start can definitely fit in an int.

C++ find size of ofstream

大憨熊 提交于 2019-12-12 09:54:05
问题 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

ifstream and ofstream or fstream using in and out

痴心易碎 提交于 2019-12-12 08:29:56
问题 When dealing with files, which of the two examples below is preferred? Does one provide better performance than the other? Is there any difference at all? ifstream input("input_file.txt"); ofstream output("output_file.txt"); vs fstream input("input_file.txt",istream::in); fstream output("output_file.txt",ostream::out); 回答1: Performance-wise, there are probably only negligible differences in this case. At best you're saving a little memory. What matters is that the first case helps with the

Does ofstream close its files automatically? [duplicate]

泄露秘密 提交于 2019-12-12 08:25:00
问题 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? 回答1: 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

std::ofstream is adding carriage return (CR; \r) after \n automatically

我怕爱的太早我们不能终老 提交于 2019-12-12 03:59:12
问题 I am trying to write PPM file on disk. PPM is a simple image format that consists of ASCII image header and byte array of pixels: P6\n width height\n 255\n [width*height*3 bytes total] This is my PPM class (simplified): class PPMImage { protected: friend std::istream& operator >>(std::istream &inputStream, PPMImage &other); friend std::ostream& operator <<(std::ostream&, const PPMImage&); size_t width; size_t height; // eg. "P6" std::string magicNumber; // Normally 255 uint16_t maxBrightness;

Closing C++ File Stream is not Opened

梦想的初衷 提交于 2019-12-12 02:47:25
问题 Suppose you declare an instance of std::ifstream or std::ofstream but is_open() returns 0 Example: std::ifstream file("myfile.txt"); if (!file.is_open()) { printf("Could not open file\n"); return; } Since the file never opened, do I still need to call file.close() after the printf statement? 回答1: No, you can only close an open file (similar to how you cannot close an already closed door - there is nothing to be done). Extra note: Please do not combine the C I/O library (X printf family of