Writing a file in collmns

hi everyone,

suppose i have data stored in some variable say, a[10]. also, i have a file which has already 2 columns X 10 rows of text in it. If i want to write a[] into the third column of the same file, how do i do it.
Read the file line by line, then append a separator at the end of each line, like a tab character, and finally append the contents of a[i], where i is the row index you currently are working.

If you want to overwrite the file's contents, read the file and write the result to a string stream (#include <sstream>), and after you are done reading, overwrite the file's contents with the contents of the stream. If you are writing a file in ANSI or UTF-8, nothing special is needed, but if you are using other encoding, remember to write the Byte Order Mark at the beginning of the file.
Effectively, the file is stored on the hard drive as a single long line of values. Being a simple text file, it will literally be something like

NUMBER SPACE NUMBER NEWLINE NUMBER SPACE NUMBER NEWLINE NUMBER SPACE NUMBER NEWLINE NUMBER SPACE NUMBER NEWLINE and so on and so on.

The file might actually be broken up a bit and spread over the hard drive, but when you open it and read it, that's how it will be presented to you. As such, there's no way to insert values - all you could do with it is write over pieces, which is no use to you.

Accordingly, you must create a new file, and delete the original.

Open the file, read the first two values, and then write those values and your new value to to the new file, then repeat for all following lines.
Last edited on
Topic archived. No new replies allowed.