从第一次使用关闭它后,我重新打开并读取一个文件SORTED.txt - 复制另一个文件UNSORTED.txt的所有内容。 从UNSORTED.txt复制后,我想要统计我复制的行数(作为单独的过程,而不是在复制过程中)。似乎fegtc()并没有指向文件的开头(SORTED.txt)第二次,因此线的值仍然是初始化为0的值。另外,通常,我可以获得重新指定fgetc()完成后没有关闭并重新打开该文件?看起来似乎得到fgetc第二次读取文件的工作
感谢您的帮助。
干杯!
f = fopen("./TEXTFILES/UNSORTED.txt", "w");
if (f == NULL){
printf("ERROR opening file\n");
return 100;
}
for (i=0; i<1000000; i++){
fprintf(f, "%d\n", (23*rand()-rand()/13));
}
fclose(f);
f = fopen("./TEXTFILES/UNSORTED.txt", "r");
if (f == NULL){
return 100;
}
s = fopen("./TEXTFILES/SORTED.txt", "w");
if (s == NULL){
return 101;
}
while(1){
j = getc(f);
if (j == EOF) break;
fputc(j, s);
}
fclose(f);
//Closed source file. Read number of lines in target file.
fclose(s);
s = fopen("./TEXTFILES/SORTED.txt", "w");
j = 0;
while(1){
j = fgetc(s);
if (j == EOF) break;
if (j == '\n') lines++;
}
fclose(s);
printf("\n%d\n", lines);
'j'是如何声明的? – cnicutar 2012-07-25 13:50:13
j被声明为int – ceod 2012-07-25 13:52:21
使用'rewind(s)'回到文件的开头[参考链接](http://www.cplusplus.com/reference/clibrary/cstdio/rewind/) – rkyser 2012-07-25 13:54:54