Solved

Amazingly silly error in Fwrite...

Posted on 2004-08-12
7
323 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

828 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