Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Windows Win32 file Text
I have written this code which reads contents of text file into a buffer and sends the buffer over the socket until end of file.
 
The code works fine and after complete file is sent to over the socket, fread() fails, however, according to my undersanding when complete file is sent over the socket, due to the condition (while(fpSend!=NULL)), fread() must not be called even Frown | :(
 
Please have a look at the code and do let me know if you see any mistake Frown | :(
 
void sendingFile()
{
   FILE *fpSend ;
   if((fpSend = fopen("Client0.txt", "r+b")) == NULL)
      {
         MessageBox( NULL,
                     "Unable to open the File",
                     "Error!",
                     MB_ICONEXCLAMATION | 
                     MB_OK);
         exit(EXIT_FAILURE);
      }
 
  char file_buffer[2000];
 
   fseek(fpSend, 0, SEEK_END);
   size_t file_size = ftell(fpSend);
   fseek(fpSend, 0, SEEK_SET);
   while(fpSend!=NULL)
   {
         int bytes_read=0;
         if((bytes_read=fread(file_buffer, 1,12, fpSend))<=0)
            {
               char err[128], bread[128];
               itoa(errno,err,10);
               itoa(bytes_read,bread,10);
               MessageBox( NULL,
                           "Unable to copy file into buffer",
                           bread,
                           MB_ICONEXCLAMATION | 
                           MB_OK);
               exit(1);
             }
         /*MessageBox( NULL,
                     file_buffer,
                     "File copied in Buffer",
                     MB_ICONEXCLAMATION | 
                     MB_OK);*/
         if(sendto(socketIdentifier, file_buffer, bytes_read, 0, (struct sockaddr *) &AH_glb_connectedSocket, sizeof(AH_glb_connectedSocket))<0)
            {
               MessageBox( NULL,
                           " NOT SENNT!",
                           "ERROR!",
                           MB_ICONEXCLAMATION | 
                                                    MB_OK);
                                     //exit(1);
            }
         else
            {
                //sent
            }
  }
   MessageBox( NULL,
        "File Sent Successfully!",
        "SENT!",
            MB_ICONEXCLAMATION | 
            MB_OK);
   fclose(fpSend);
}
Posted 9-May-13 22:32pm
Comments
Richard MacCutchan at 10-May-13 4:18am
   
You allocate a 2000 byte buffer, you then get the file's size, but then read only 12 bytes at a time. Why not transfer larger blocks?
 
Are you also saying that the call to exit(1); does not get executed?
ayesha hassan at 10-May-13 4:57am
   
My mistake, thank you for the suggestion :)
Richard MacCutchan at 10-May-13 4:21am
   
Also, your options to fopen() should be "rb" not "r+b".
ayesha hassan at 10-May-13 4:56am
   
even "r+b" is working fine. Can you explain a little further why do you prefer "rb"?
Richard MacCutchan at 10-May-13 5:59am
   
"r+" means the file is open for writing as well as reading; since you are only reading it you should use just "r".
ayesha hassan at 10-May-13 6:02am
   
Got that. Thank you :)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your while condition is uninfluential (always true). According to the documentation[^]:

Return Value
The total number of elements successfully read is returned.
If this number differs from the count parameter, either a reading error occurred or the end-of-file was reached while reading. In both cases, the proper indicator is set, which can be checked with ferror and feof, respectively.
 
you should instead check the fread return value.
  Permalink  
Comments
ayesha hassan at 10-May-13 4:54am
   
while(fpSend!=NULL)
{
int bytes_read=0;
if((bytes_read=fread(file_buffer, 1,12, fpSend))<=0)
{
if(feof(fpSend))
{
//"End of File Found";
return 0;
}
else
{
//"fread() failed"
return 0;
}
}
Is it what you meant?
CPallini at 10-May-13 5:02am
   
Something like that, however, your function should close the file, before returning.
ayesha hassan at 10-May-13 5:34am
   
Always helpful :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 215
1 George Jonsson 175
2 Kornfeld Eliyahu Peter 159
3 PIEBALDconsult 110
4 Richard MacCutchan 85
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 10 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100