I'm trying to take information from a text file and read it in, in my main i get the length of the file in bytes, as well as the name of the file to ensure that it exists, and pass it to this method.
I have some test code in there for seeing where the issue is, but I can not seem to see where the seg fault comes from, the fgetc return value matches the value its stored in which is the only thing I can think of.
uint32 getCode(char *fileA, int count){
//creating variables to store the data we are reading in
int buffer = 0;
register uint64 total = 0;
//opening with rb ensures that all file types will be readable
FILE *file;
if(file= fopen(fileA, "rb")){
printf("\nfilename: %s\ncount: %d\n",fileA,count);
}
// while loop reading in 32 bits at a time or 4 bytes
while(count > 0){
buffer = fgetc(file);
count--;
printf("\n%s", buffer);
}
fclose(file);
return 1;
}
The segmentation fault comes from printf("\n%s", buffer);
-- your buffer
is not a string, and you may get much better results printing using format '%c'
And yeah, the comment // while loop reading in 32 bits at a time or 4 bytes
is a bit misleading, since you're reading 1 byte at a time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.