Unwanted characters written during string write to a binary file
I have a program that walks the file system hierarchy listing the file path names on a binary file as follows:
struct list_rec{
int seqno;
int path_length;
char *file_path;
};
FILE *listoffiles;
int no_of_files;/*couter to keep track of files visited*/
static int list_the_files(const char *fpath,const struct stat *sb, int tflag, struct FTW *ftwbuf)
{
struct list_rec t;
no_of_files = no_of_files+1;
t.seqno = no_of_files;
t.path_length = strlen(fpath);
t.file_path = malloc((sizeof(char)*t.path_length)+1);
strcpy(t.file_path,fpath);
fwrite(&t.seqno,sizeof(int),1,listoffiles);
fwrite(&t.path_length,sizeof(int),1,listoffiles);
fwrite(t.file_path,t.path_length,1,listoffiles);
free(t.file_path);
return 0;
}
int main(int argc, char *argv[])
{
listoffiles = fopen("/home/juggler/Examples/Traces/files.txt","r+b");
no_of_files = 0;
ftw(argv[1],list_the_files,20);
}
and then read the file to check from a different program in the following way:
struct list_rec{
int seqno;
int path_length;
char *file_path;
};
FILE *listoffiles;
int main()
{
struct list_rec t;
listoffiles = fopen("/home/juggler/Examples/Traces/files.txt","rb");
if(!listoffiles)
{
printf("Unable to open file");
return 1;
}
while(fread(&t.seqno,sizeof(int),1,listoffiles)!=0)
{
printf("\n %d ",t.seqno);/*Log Record Number*/
fread(&t.path_length,sizeof(int),1,listoffiles);
printf(" %d",t.path_length);
开发者_如何学运维 t.file_path = malloc(sizeof(char)*t.path_length);
fread(t.file_path,t.path_length,1,listoffiles);
printf(" %s",t.file_path);
fflush(stdout);
}
}
My problem is the output on running the second program shows some unwanted characters appended to some of the filepaths as follows:check record 51611 and 51617
51611 92 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/document6_new.pdf��
51612 99 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/proof with graph.tex.sav
51613 115 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/Operations beyond read and write.tex.bak
51614 107 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/singe version implementation.blg
51615 93 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/formalization1.pdf
51616 92 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/document6_new.texÉ…
51617 95 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/cascading undone.tex
You're not null-terminating the file paths when you read them back in.
You should be allocating one more char's worth than the string length, and setting that last char to 0 (fread
won't do that for you).
You're not writing the terminating NULL for file_path
(or counting the space required for it) therefore when you read it back in .. it's not null terminated.
t.file_path = malloc(sizeof(char)*t.path_length);
fread(t.file_path,t.path_length,1,listoffiles);
printf(" %s",t.file_path);
from the code it's clear that you forgot to append null character to the end of t.file_path. malloc doesn't initialize data pointed by the returned pointer.
精彩评论