为什么这个程序一次又一次不显示第一行?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *readLine(FILE *inFile) //Simply reads line in a text file till "\n"
{
char *line = realloc(NULL, 1);
char c;
int i=0;
while (!feof(inFile))
{
c = fgetc(inFile);
if (ferror(inFile)) printf("Error reading");
if (c == 10)
{
realloc(line,i+1);
line[i]= 10;
break;
}
realloc(line, i+1);
line[i++] = c;
}
return line;
}
int main(int argc,char **argv)
{
FILE *inFile;
inFile = fopen("testFile","r");
printf("%s",readLine(inFile));
printf("%s",readLine(inFile));
printf("%s",readLine(inFile));
return 0;
}
如果 testFile 的内容是:-
abc
def
ghi
三个 printf 语句应该显示“abc”三次。但是输出是:-
abc
def
ghi
我知道我在某个地方的概念是错误的。请帮忙。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *readLine(FILE *inFile) //Simply reads line in a text file till "\n"
{
char *line = realloc(NULL, 1);
char c;
int i=0;
while (!feof(inFile))
{
c = fgetc(inFile);
if (ferror(inFile)) printf("Error reading");
if (c == 10)
{
realloc(line,i+1);
line[i]= 10;
break;
}
realloc(line, i+1);
line[i++] = c;
}
return line;
}
int main(int argc,char **argv)
{
FILE *inFile;
inFile = fopen("testFile","r");
printf("%s",readLine(inFile));
printf("%s",readLine(inFile));
printf("%s",readLine(inFile));
return 0;
}
If the contents of testFile is:-
abc
def
ghi
The three printf statements should show "abc" three times.. But the output is:-
abc
def
ghi
I know I am wrong in the concept somewhere. Pls help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
realloc()
的使用不正确。因为
line
是按值传递的,所以它不会改变。实际重新分配的内存在返回值中。因此编辑:刚刚意识到即使如此这是一个错误,不会导致重复行。其他要点仍然有效。line
一遍又一遍地保持相同,并且您一遍又一遍地看到同一行。更糟糕的是:
line
值在重新分配后可能会变得无效(如果它被重新分配到堆的不同部分)。Usage of
realloc()
is incorrect.Because
line
is passed by value, it's not changed. The actual re-allocated memory is in the return value.ThereforeEdit: just realized that's even though it's a bug, it's not what would cause repeating lines. Other points are still valid.line
remains the same over and over again, and you are seeing the same line over and over again.What's worse:
line
value may become invalid after reallocation, if it was reallocated in a different part of the heap.啊,我理解你的困惑。
文件指针仅指向实际的文件结构。当前偏移量等状态不是指针的一部分,而是内部结构的一部分。
另一种思考方式是表示文件的实际对象是 FILE。要获得按引用传递语义,您需要传递一个指向该对象的指针。由于您是通过引用传递的,因此每一行都会从上一行结束的地方继续。
Ah, I understand your confusion.
A file pointer only points to the actual file structure. State such as the current offset are not part of the pointer but are part of the internal structure.
Another way to think about this is that the actual object representing the file is FILE. To get pass-by-reference semantics, you pass a pointer to the object. Since you are passing by reference, each line picks up where the last one left off.
fgetc()
前进文件指针(即“要读取的下一个字符所在的位置”)。这就是您如何能够在循环中调用它并读取整行字符的方式。当它前进到换行符之后,它自然地移动到下一个字符,即下一行的开头。
您可以使用
fseek()
函数修改文件指针。例如,调用fseek(inFile, 0, SEEK_SET)
会将其重置为文件开头,导致下一个fgetc()
调用从第一个字符开始文件的。fgetc()
advances the file pointer (which is "where the next character to be read is located"). That's how you're able to call it in a loop and read a whole line of characters.After it advances past the newline character, it naturally moves on to the next character, which is the beginning of the next line.
You could modify the file pointer with the
fseek()
function. For example, callingfseek(inFile, 0, SEEK_SET)
would reset it to the beginning of the file, causing the nextfgetc()
call to start over from the first character of the file.