c - When reading from a file I get special characters, that are not in my text file -
for starters, don't know if has os on osx. when running program , printing out read text file following:
a b c d e f g h j k l m n o p q r s t u v w x y z b c d e f g h j k l m n o p q r s t u v w x y z\353`\232\377
i reading new text document created using text wrangler, turned on invisibles make sure there wasn't @ end of file. code using read text file here:
// parses text file, passes parsed text writeandencode writing , encryption. int encode(char *fileread, char *filewrite) { // file file *filetoread = fopen(fileread, "r+"); // open file reading char *textwithinfile; // check if file can read if (fileread != null) { // file can read. // length of file fseek(filetoread, 0l, seek_end); size_t size = (size_t)ftell(filetoread); rewind(filetoread); // make sure no error in finding size of file. if (size != -1) { long charsize = sizeof(char); textwithinfile = malloc(sizeof(char) + size); // add text filetoread char array fread(textwithinfile, charsize, size, filetoread); // add null @ end make file string textwithinfile[size] = '\0'; printf("%s\n", &textwithinfile[0]); fclose(filetoread); // debugging find out being read writeandencode(size, textwithinfile, filewrite); free(textwithinfile); } else { //file can't read printf("***error_file_to_read_size_can_not_be_found***\n"); } } else { printf("***error_file_to_read_can_not_be_read***\n"); } return 0; }
would have same problem if on windows?
anyhow thank help!
i've fixed code little , added debugging statements. it'll save contents of input file malloc
'ed buffer of size=(charsize*filesize+1)
, +1
bit hold null terminating character. works on machine reasonably sized binary files
you can uncomment printf(buffer_copy)
statement doing before. otherwise, it'll loop through each byte in buffer , output hexadecimal equivalent. if you're still getting 'junk' it's part of input file , not bug.
//get file file *infp=fopen(infile,"r"); if(infp!=null){ //get length of file fseek(infp,0l,seek_end); size_t filesize=(size_t)ftell(infp); printf("file length = %d\n",(int)filesize); //debug statement rewind(infp); if(filesize>0){ char * buffer; buffer=(char*)malloc((sizeof(char)*filesize)+1); // +1 null char @ end size_t chars_read=fread(buffer,sizeof(char),filesize,infp); printf("chars read = %d\n",(int)chars_read); // debug statement (chars_read should equal filesize) buffer[filesize]='\0'; // terminate char array fclose(infp); //output read (method 1, print string) //char *buffer_copy=buffer; //printf("the file=\"%s\"",buffer_copy); //uncomment these 2 statement did before */ //output read (method 2, byte-by-byte) if(chars_read>0){ int i; for(i=0;i<chars_read;i++){ char the_char = *buffer; printf("char%d=%02x\n",i,the_char); //output each byte hexadecimal equivalent buffer++; } } else { printf "problem fread"; } } else { printf("problem filesize"); } else { printf("problem opening file"); }
the while
loop stop reading @ first null terminating char. for
loop read every single byte inside file (in case you're trying peek inside isn't .txt
, .jpg
)
have tried checking file command line make sure has characters expect?
for example, running command od -c
view each byte ascii equivalent (or octal, if non-printable).
Comments
Post a Comment