C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
fseek failure in Borland compilers?!?!?! Posted by gambit1724 on 10 Apr 2002 at 7:45 AM
Hi, I'm writting a text editor in C, and I have a problem.
I isolated the problem to the next simple program:
first, I work with bin files,and when writting to the file or reading,
it's by Str[73].
suppose that we prepared a file with three strings in it,the length of each is 73. now see this program:
void main()
{
FILE *fp;
char Str[73];

fp=fopen("FilePath","r+b");
fread(Str,sizeof(char),73,fp);
fseek(fp,-73L,SEEK_CUR); //return to the beginning of FirstLine
fwrite(Str,sizeof(char),73,fp);
fread(Str,sizeof(char),73,fp); //fail
fread(Str,sizeof(char),73,fp); //fail
fclose(fp);
}
now, I've discovered that putting fseek(fp,ftell(fp),SEEK_SET)
after the fwrite solves the problem.
if anyone knows info that can be help,I will be glade to here.
thx,
gambit

and sorry for my bad english

Report
Re: fseek failure in Borland compilers?!?!?! Posted by whoie on 10 Apr 2002 at 8:21 AM
: Hi, I'm writting a text editor in C, and I have a problem.
: I isolated the problem to the next simple program:
: first, I work with bin files,and when writting to the file or reading,
: it's by Str[73].
: suppose that we prepared a file with three strings in it,the length of each is 73. now see this program:
: void main()
: {
: FILE *fp;
: char Str[73];
:
: fp=fopen("FilePath","r+b");
: fread(Str,sizeof(char),73,fp);
: fseek(fp,-73L,SEEK_CUR); //return to the beginning of FirstLine
: fwrite(Str,sizeof(char),73,fp);
: fread(Str,sizeof(char),73,fp); //fail
: fread(Str,sizeof(char),73,fp); //fail
: fclose(fp);
: }
: now, I've discovered that putting fseek(fp,ftell(fp),SEEK_SET)
: after the fwrite solves the problem.
: if anyone knows info that can be help,I will be glade to here.

An explicit call to either fseek() or fflush() is required between reads and writes in update ('+') mode. Read Question 12.30 in the C FAQ (second link in my signature) for more info.


: thx,
: gambit
:
: and sorry for my bad english

Your English is fine! :)


HTH,
Will
--
http://www.tuxedo.org/~esr/faqs/smart-questions.html
http://www.eskimo.com/~scs/C-faq/top.html
http://www.parashift.com/c++-faq-lite/
http://www.accu.org/





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.