Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C++ Libraries / August 2006

Tip: Looking for answers? Try searching our database.

A error with fwrite?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dwbclz - 16 Aug 2006 12:35 GMT
The following is my code:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
   char buf[128];
   memset(buf, 0, sizeof(buf));

   FILE *fp = fopen("prepatch.log", "wb+");
   fwrite(buf, 1, sizeof(buf), fp);
   fseek(fp, 0, SEEK_SET);

#if 1
   fread(buf, 1, 8, fp);
#else
   fseek(fp, 8, SEEK_CUR);
#endif

   fwrite("test", 1, 4, fp);
   fclose(fp);
   return 0;
}

I cannot write "test" to the file if I read it.
But fseek does correct.

I find that 'FILE::_flag' is not set correctly.
So _flush does not flush the buffer.

VC2003 and VC2005 is same.
Doug Harrison [MVP] - 16 Aug 2006 16:08 GMT
>The following is my code:
>
[quoted text clipped - 28 lines]
>
>VC2003 and VC2005 is same.

You need the fseek between the read and the write for the reason given
here:

Stream States
http://msdn2.microsoft.com/en-us/library/kb1at4by.aspx
<q>
You cannot call a write function if the last operation on the stream was a
read, unless that read operation set the end-of-file indicator.
</q>

Signature

Doug Harrison
Visual C++ MVP


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.