Newbie: can I rewrite data with fwrite?

Z

Zalek Bloom

I created a binary file with commands:

FILE * fStudent ;
Student s; // this is a class

fStudent = fopen("student.txt", "a+b") ;

cnt_w = fwrite(&s, sizeof(s),1,fStudent) ;

fclose(fStudent);

I wrote a few records and viewed the file - file was OK. Now I wanted
to rewrite some records. I positioned a file pointer on a specific
record and issued a fwrite command. When I displayed the rewritten
file I noticed that a specific records was not changed. Notning was
changed in the file.

My question:
can I rewrite some data in the middle of file using fwrite command?
If not - which command will allow me to rewrite data?

Thanks,

Zalek
 
N

Noah Roberts

Zalek said:
I created a binary file with commands:

FILE * fStudent ;
Student s; // this is a class

fStudent = fopen("student.txt", "a+b") ;

Are you also attempting to open the file with these arguments when you
are trying to change? You have opened in append mode, not write mode.
cnt_w = fwrite(&s, sizeof(s),1,fStudent) ;

fclose(fStudent);

I wrote a few records and viewed the file - file was OK. Now I wanted
to rewrite some records. I positioned a file pointer on a specific
record and issued a fwrite command. When I displayed the rewritten
file I noticed that a specific records was not changed. Notning was
changed in the file.

My question:
can I rewrite some data in the middle of file using fwrite command?
If not - which command will allow me to rewrite data?

I am not complete sure what happens with the pointer when opening in
read+append mode like you have done. From the manual page I would
gather that you can't set the pointer to a location of a previously
written entry if you open in this mode. You should be able to issue an
fwrite over an existing entry assuming you have the pointer at the
correct location. It could simply be a matter of the wrong mode.

NR
 
L

llewelly

Noah Roberts said:
Are you also attempting to open the file with these arguments when you
are trying to change? You have opened in append mode, not write
mode.

Append mode is a write mode.
I am not complete sure what happens with the pointer when opening in
read+append mode like you have done. From the manual page I would
gather that you can't set the pointer to a location of a previously
written entry if you open in this mode.

This is correct; fseek and similar will have no effect on a file
opened in append mode.
You should be able to issue
an fwrite over an existing entry assuming you have the pointer at the
correct location. It could simply be a matter of the wrong mode.

It is. You can't do that in append mode. You can in w mode.
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top