Reputation: 8173
Hello every one this is extension to my previous question.Here is the link
I have inserted a '/n' at the end of my file name and successfully retrieve the file name along with the data in the file. but there is some problem that after my server side read the bytes from sockets up to '/n' it also write a strange statement in start of my file
ORBIT_SOCKETDIR=/tmp/orbit-cv
can any one tell me what is happening. how to avoid it in storing in my file
here is my code snippets client.c
char * filename = strrchr(argv[3],'/'); // extracting file name from path
*filename++; filename[strlen(filename)] = '\n'; // putting /n at the end of file name n = write(sockfd, filename, strlen(filename));
bzero(buffer,256); FILE *f = fopen("file.txt" ,"rb"); // opening the file
size_t bytes = 0;
// sending the actual data in the file
while((bytes = fread(buffer ,sizeof(char) ,sizeof(buffer) ,f))>0)
send(sockfd ,buffer ,bytes , 0);
server.c
while(ch[0] != '\n') // here reading the filename
{
n = read(newsockfd,ch,1);
filename[i] = ch[0];
printf("ch %c " , ch[0]);
i++;
}
FILE *f = fopen(filename ,"wb");
// reading the actual data and writing it in the file
while((bytes = recv(newsockfd ,buffer , sizeof(buffer) ,0))>0)
fwrite(buffer,sizeof(char) ,bytes , f);
Upvotes: 1
Views: 109
Reputation: 500437
You're trying to append a character to argv[3]
in-place. However, there's no guarantee that enough memory has been allocated for it to accommodate the extra character.
Also, once you've replaced the NUL
terminator with a \n
, you're forgetting to add a new NUL
terminator.
In summary: allocate enough memory for argv[3]
plus \n
plus a NUL
, copy the string into it, and finally append \n
and NUL
.
Upvotes: 2