问题
I am working in C and have a text file that is 617kb that I am trying to read with fgetc
. For some reason fgetc
is starting randomly within the file. I have tried moving the file pointer to get beginning with fseek
with no success. I can get fgetc
work to fine with smaller files. Any help is appreciated.
Sample input is 25,000 lines of data similar to:
Product
23 660
2366 3
237 09
2 3730
23734
23 773
241 46
Source:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void print(FILE *category){
int ch = 'a';
while ((ch = fgetc(category)) != EOF){
printf("%c", ch);
}
getchar();
}
int main(void){
FILE *category = fopen("myFile", "r");
if (category == NULL){
puts("category file not found");
}
else{
print(category);
fclose(category);
return 0;
}
}
回答1:
I suspect the problem lies elsewhere.
Where is the output from this program going? If you're sending it to the console, its scrollback buffer won't be large enough to contain the whole file. Maybe it just looks like fgetc()
is starting in an odd place.
Try diverting the output from this program to a new text file and compare the size of this file with the size of the input file, e.g.:
./category_print >output.txt
来源:https://stackoverflow.com/questions/20341044/fgetc-not-starting-at-beginning-of-large-txt-file