I need to do the following in a .csv file using a c++ code
1. open the file
2. read the first line...extract the number part of it
3. enter the user entered data at the end of the file (append)
4. now add the extracted integer and a certain user inoutted integer and replace the number part of the first line with this new integer.

I have done the most of it...what remains is how to replace the first line?

Recommended Answers

All 4 Replies

I have done the most of it...what remains is how to replace the first line?

There are two options:

1.
- read the original file in your memory.
- delete the file
- create a new file and add your line
- add all the lines in memory

2.
- rename the original file
- open a new file and add your line
- open the original and write the lines to the new file

there has to be a better way then deleting (or creating copies of ) the old file completely every time you need to amend something in it.

If you append on the back you wouldn't have these issues

there has to be a better way then deleting (or creating copies of ) the old file completely every time you need to amend something in it.

Nope, there isn't, unless you are using absolute fixed-length fields, and 99% of the time, a .csv file does not have fixed length fields.

If you want to change a value in the first line, you read the file, modify the first line, and write it out again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.