Read and write the same file

Y

YUS

First of all, I'm a c++ beginner.. as u will see..

I'd like to do this:

- The code open a file
- it searchs for the string "BUDGET = "
- the budget value is overwritten or at least the entire row.

CODE:

fstream file;
memset(buf,0,512);
file.open(nomefile, ios::eek:ut | ios::in);
do{ file.getline(buf, 512, '\n');
} while(!strncmp(buf, "BUDGET", 6));

file << "BUDGET = " << budget << endl;

The problem is that the file is not written, no changes are made to the
file.
I've also tried with ios::app but with no results.

How I can do ?
Thanks to all.
 
J

Jon Rea

YUS said:
First of all, I'm a c++ beginner.. as u will see..

I'd like to do this:

- The code open a file
- it searchs for the string "BUDGET = "
- the budget value is overwritten or at least the entire row.

CODE:

fstream file;
memset(buf,0,512);
file.open(nomefile, ios::eek:ut | ios::in);
do{ file.getline(buf, 512, '\n');
} while(!strncmp(buf, "BUDGET", 6));

file << "BUDGET = " << budget << endl;

The problem is that the file is not written, no changes are made to the
file.
I've also tried with ios::app but with no results.

How I can do ?
Thanks to all.

You need to close the filestream to write the current memory to the file...

Jon Rea
 
T

TB

YUS sade:
First of all, I'm a c++ beginner.. as u will see..

I'd like to do this:

- The code open a file
- it searchs for the string "BUDGET = "
- the budget value is overwritten or at least the entire row.

CODE:

fstream file;
memset(buf,0,512);
file.open(nomefile, ios::eek:ut | ios::in);
do{ file.getline(buf, 512, '\n');
} while(!strncmp(buf, "BUDGET", 6));

file << "BUDGET = " << budget << endl;

The problem is that the file is not written, no changes are made to the
file.
I've also tried with ios::app but with no results.

How I can do ?
Thanks to all.

I believe, IIRC, that you after a read must do
a seekp() before you write, and vise versa (but with
seekg()).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top