I am transferring a file from client to server using C++ socket programming on linux. I have successfully transferred the file. The only problem is some extra characters are also getting transferred at the end of the file. How can I deal with this..??
Plz reply now if possible..thanks in advance
Regards,
Aditi
This is file new.txt. You can see in the end some garbage values are getting printed. This is file received at server side :(
//file new.txt
#include"stdio.h"
int main()
{
int wc = 0;
FILE *fp;
int c;
fp = fopen("mee.txt","r");
while(1)
{
c = fgetc(fp);
if(c == EOF)
break;
else
{
if( c == ' ')
wc = wc + 1;
}
}
printf("Number of words are %d\n",wc);
fclose(fp);
return 0;
}
q
problem resolved. I had used fseek function as follows.
fseek(f,0,SEEK_END);
to calculate size of the file f is pointing to at client side as:
lsize = ftell(f);
After which i was initializing my buffer as :
filebuffer = (char *)malloc(sizeof(char) * (lsize));
which is incorrect bcoz lsize-1 is actual size of file since SEEK_END points to end of file
Instead,
filebuffer = (char *)malloc(sizeof(char) * (lsize-1));
is right
This is client side code.. If i pass lsize as argument to fread here then some extra special characters are printed at server side. SendData is a function which uses send() inside.
fread() doesn't append a '\0' at the end of the read data, but strlen() needs the final 0.
Replace strlen(filebuffer) with lsize and you're done. And remove the -1