Solved

Amazingly silly error in Fwrite...

Posted on 2004-08-12
7
320 Views
Last Modified: 2010-04-15

 Fwrite returns 0 and does not write anything but the error code is zero.. Any guesses why???

0
Comment
Question by:shubha_mohan
  • 4
  • 2
7 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
Code please?
0
 

Author Comment

by:shubha_mohan
Comment Utility
FILE * fPointer; // Global.

 int func() {

      char temp[5]="hello";
      int num;
            
      fPointer = fopen("ISEConfig.Txt","rw");
      
      if (fPointer == NULL)
      {
            LogEvent(0,ghwnd,CONFIGFILEABSENT);
            return FALSE;
      }

      //Move pointer to the beginning of the file
      
      fseek(fPointer,0,0);
      IFD_PORT =0;
      // Doing some reading of characters that i want..
      while(chRead != feof)
      {
            fread(&chRead,sizeof(char),1,fPointer);
            if (isdigit(chRead))
            {
                  if (IFD_PORT ==0) IFD_PORT = chRead -'0';
                                                break;

            }

      }
                //now trying to write
      num = fwrite(temp,5,5,fPointer);
      LogEvent (2,ghwnd," Wrote %d items...Error %d ",num,GetLastError());
      fclose (fPointer);
}
0
 

Author Comment

by:shubha_mohan
Comment Utility
Simpler code !!
void main() {

     char temp[15]="11111111111111";
     int num;
     char chRead;
         
     fPointer = fopen("C:/Development/configtestspl/ISELog.txt","rw");
     
     if (fPointer == NULL)
     {
          printf(" File OPen Error ");
          return ;
     }

     //Move pointer to the beginning of the file
     
     fseek(fPointer,0,0);
   
                //now trying to write
     num = fwrite(temp,15,15,fPointer);
     printf(" Wrote %d items...Error %d ",num,GetLastError());
     fclose (fPointer);
}

Same problem !!! :(( The file that i am attempting to write into is empty
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
fwrite returning 0 does not mean succesful execution.

On success, fwrite returns the number of items (not bytes) actually written.
On error, fwrite returns a short count.

So if there is no error,0 items have been written
0
 

Author Comment

by:shubha_mohan
Comment Utility
Yup... I am trying to figure out why it is not writing anything :((  
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 50 total points
Comment Utility
It works if you change the mode to "w".
Use "r+w" if you want to read and write at the same time.
0
 

Author Comment

by:shubha_mohan
Comment Utility
Wow cool.. Thanks :)) That is really really silly of me !!!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now