Solved

Amazingly silly error in Fwrite...

Posted on 2004-08-12
7
322 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
ID: 11781867
Code please?
0
 

Author Comment

by:shubha_mohan
ID: 11781902
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
ID: 11782275
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 9

Expert Comment

by:ankuratvb
ID: 11782288
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
ID: 11782300
Yup... I am trying to figure out why it is not writing anything :((  
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 50 total points
ID: 11782306
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
ID: 11782316
Wow cool.. Thanks :)) That is really really silly of me !!!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 while-loops in the C programming language.

786 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