c - fread() fails after reading complete contents of text file -
i have written code reads contents of text file buffer , sends buffer on socket until end of file.
the code works fine , after complete file sent on socket, fread() fails, however, according undersanding when complete file sent on socket, due condition (while(fpsend!=null)), fread() must not called :(
please have @ code , let me know if see mistake :(
void sendingfile() { file *fpsend ; if((fpsend = fopen("client0.txt", "r+b")) == null) { messagebox( null, "unable open 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 copy file 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); }
the file handle fpsend
doesn't become null
after end of file. should check feof(fpsend);
.
also, using ftell();
determine size of file works if file less 2gb in size because returns 32 bit signed int (size_t).
Comments
Post a Comment