outputting to a csv

Lets say I have a csv and want to first output data to the third cell and then go back and and post data to the first cell is this possible ? View the short example for more clarification below. Thanks!
1
2
3
// open the file for output etc
outputfile << "," << "," << "3rd cell"; 
// now how can I go back and output something to the first or second cell after skipping the first two cells in the line above?  
not really possible.

you can't "insert" something into a file, you would have to move the rest of the file contents to make the space for it.

If you need to change the contents then you will need to resave the entire file.



No, it's not possible, especially with text files, unless you have pre-allocated a fixed amount of blank or otherwise overwritable space for the data.

Files are usually written sequentially except in specific cases (for example, there is a 4-byte space reserved in a WAV file format (binary file) where you can write the size of the data stream, this lets you write the size of the data stream after you calculate the data stream, but again you need to already know you allocated 4 bytes in advance).

Do all the necessary logic first, then write to the output stream. This is easier to maintain anyway than what you're suggesting.
Last edited on
Hold on guys. The data of a csv file uses a ',' as a delimiter. Right? Couldn't you create a function that reads the data from the csv and stores it into a vector and also use a comma to move on to the next element of the vector? Say a vector of strings. The csv cell 1 would be vector[0] and so on. Then if you wanted to modify cell 2 for example, you could modify vector[1]. Then before closing the file stream you would write all the data from your vector back into the csv. Something like that maybe?
Okay thank you everyone. And manga I think that would work but with the size of my actual program something like that wouldn't work, atleast for my application :)
@Manga
Not sure what you mean, but it sounds like you're still not writing anything to the file until the end.
@manga, thats the only way to do it. load the entire file into memory, make the changes, save the entire file to storage.

of course, that means the application should really work with the loaded data and just save it to csv whenever it changes.

which is a whole other ball game to editing the content of a file.
Last edited on
@Ganado
Yes that is pretty much what I meant. I suppose a neat party trick for a small file, but if the data is massive, then things get ugly. :(
Topic archived. No new replies allowed.