Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Amazingly silly error in Fwrite...

Posted on 2004-08-12
7
Medium Priority
?
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

610 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