Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :(

Please have a look at the code and do let me know if you see any mistake :(

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
Comments
Richard MacCutchan 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 10-May-13 4:57am    
My mistake, thank you for the suggestion :)
Richard MacCutchan 10-May-13 4:21am    
Also, your options to fopen() should be "rb" not "r+b".
ayesha hassan 10-May-13 4:56am    
even "r+b" is working fine. Can you explain a little further why do you prefer "rb"?
Richard MacCutchan 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".

1 solution

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.
 
Share this answer
 
Comments
ayesha hassan 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 10-May-13 5:02am    
Something like that, however, your function should close the file, before returning.
ayesha hassan 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900