C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Read and write the same file

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
YUS
Guest





PostPosted: Mon Jan 30, 2006 5:01 pm    Post subject: Read and write the same file Reply with quote



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::out | 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.

--
oooO Oooo
( ) YUS ( )
\ ( Para todos, todo, ) /
\_ ) nada para nosotros. ( _/
Back to top
TB
Guest





PostPosted: Mon Jan 30, 2006 5:01 pm    Post subject: Re: Read and write the same file Reply with quote



YUS sade:
Quote:
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::out | 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()).

--
TB @ SWEDEN
Back to top
Jon Rea
Guest





PostPosted: Mon Jan 30, 2006 5:01 pm    Post subject: Re: Read and write the same file Reply with quote



YUS wrote:
Quote:
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::out | 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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.