I have a few question re manipulating text files using fstream.
In my project I need to convert one file format to another (basically just adding a header to the new file). However part of the header requires me to specify the number of lines in the original file - something I can only know after I have looped through the entire file.
Q1 - Is there a way that I can open a file and insert lines at a) a certain point in the file ie line 10, b) insert lines at the start of the file. I only found reference to being able to append ios::app
Q2 - Is there a way of looping through the file to increment a counter. I tried
but this just went into an endless loop. I believe because I'm not actually reading anything in. Obviously doing this I would then close the file, and then reopen it for reading. Inefficient but I need the number of lines to be in the header. I could just read something in but again this takes time.Code:
Thanks for comments
PS - Q3 - I do a read from my file ie finput >> x >> y >> z; Now my original file has an empty line at the end, and the x,y,z values therefore have the previous entry. Whats the best way of ensuring catering for this situation?