• C

Amazingly silly error in Fwrite...


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

shubha_mohanAsked:
Who is Participating?
 
ankuratvbCommented:
It works if you change the mode to "w".
Use "r+w" if you want to read and write at the same time.
0
 
Sjef BosmanGroupware ConsultantCommented:
Code please?
0
 
shubha_mohanAuthor Commented:
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
shubha_mohanAuthor Commented:
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
 
ankuratvbCommented:
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
 
shubha_mohanAuthor Commented:
Yup... I am trying to figure out why it is not writing anything :((  
0
 
shubha_mohanAuthor Commented:
Wow cool.. Thanks :)) That is really really silly of me !!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.