- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
struct node
{
char item[200];
struct node* next;
};
FILE* unionMethod(char f1name[50], char f2name[50])
{
FILE* f1;
FILE* f2;
f1=fopen(f1name,"r");
f2=fopen(f2name,"r");
char line[200];
struct node *head1=NULL;
struct node *head2=NULL;
while(!feof(f1))
{
fgets(line, 200, f1);
struct node *cur=head1->next;
if(head1==NULL)
{
head1 = (struct node *) malloc( sizeof(struct node));
fgets(line,200,f1);
strcpy(head1->item, line);
head1->next=NULL;
}
else
{
cur = (struct node *) malloc( sizeof(struct node));
fgets(line,200,f1);
strcpy(cur->item, line);
cur->next=NULL;
}
}
fclose(f1);
}
int main()
{
unionMethod("inputfile1.txt","inputfile2.txt");
return 0;
}
我基本上想做的是从文件中获取行并将它们放入链接列表中。然而,当执行到达同时零件程序停止时,由于“feof”功能。我无法理解这个问题。
感谢您的帮助。
最佳答案
这是一个非常频繁的question ,只需更改
while (!feof(f1))
至
while (fgets(line, 200, f1))
原因是 EOF
标记是在 fgets()
尝试读取文件末尾之后设置的,因此您将需要一次额外的迭代来完成它已设置。
关于c - C 中的 Feof 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28396687/
这是一个更大的电影 Sprite 程序的一部分,由于某种原因,程序在到达这个 while 循环时崩溃了,我不明白我的问题是什么。 #include #include #include #incl
这个问题已经有答案了: feof detecting false end of file (2 个回答) 已关闭10 年前。 我有一个函数应该读取文件并检测文件何时结束。 该函数当前仅读取倒数第二行并
我之前问过一个不同的问题,但我对这个问题的看法有点偏题,所以我提出了一个新问题,因为我问的是一个完全不同的问题。 我有一个函数可以读取文本文件中的给定行(由 ac 变量给出)。它执行行的读取,然后检查
大家好,我正在编写一个 c 程序,我试图一次从一个文本文件中打印两行,问题是当在最后一行时,如果行是奇数,3 行 5 行。它会打印最后一行两次。我找不到针对这个特定问题的 if 函数的比较。是 boo
这个问题在这里已经有了答案: Why is “while( !feof(file) )” always wrong? (5 个答案) 关闭 5 年前。 我是 C 的新手。我偶然发现了 feof 的某
我正在尝试通过套接字发送文件,方法是一次读取一个字符,直到缓冲区(字符数组)已满,发送它,然后重复直到文件末尾。但出于某种原因,feof 在文件结束前变为真(我相信可能是在缓冲区数组已满时)。 int
我是 C++ 的初学者,正在努力更好地理解 feof()。我读过 feof() 标志仅在尝试读取文件末尾后才设置为 true 很多次初学者如果执行类似 同时(!feof(文件))。不过,我想了解的是,
所以我做了一件简单的事情,首先我通过 ssh2_exec 执行命令(在成功验证后),然后读取变量中的答案。下面是我的代码(没有验证) try { $stdout_stream = ss
This link讲述为什么 feof() 用作循环的退出指示器是一件坏事。 不安全==>在 while 中进行 feof() 检查,并在 while 内进行 fgets() 检查。 安全==>让fg
我的程序中出现错误(应该找到字符和整数文件中的所有整数),其中“feof”不起作用(while循环永远不会结束)或没有读取/找到整数,除非整个文件充满了整数。 我的代码... #include
void suffix_strip(FILE *fp) { FILE *fp1; char word[20],ch; int len; int i=0,j; f
我正在使用 feof 计算文件中的行数,如下所示。完成后,我需要逐行循环遍历同一个文件,并执行另一个取决于行数的操作。但是,while (!feof(f)) 不会在同一个文件流上运行第二次。有没有办法
我有以下枚举和结构: enum Destination { unknown = 0, hosok, parlament, var }; struct Client { char name[30];
我正在使用 fgets(); 从文本文件中读取行 我有一个循环:while(!feof(fp)) 问题是,即使在我到达并读取最后一行之后,程序仍继续进入循环,并且在调试中文件 ptr 仅显示点而不是数
我这里真的有问题。从文件中读取字符时,我似乎并没有真正找到退出循环的最佳方法。我知道每个教程都建议我不应该使用 while( !feof() ) 但他们除了在 while 中放置 fgets() 之外
FILE *test; student st; int i = 0; test = fopen("example.bin", "rb"); while (feof(test)) { fread
我在从文件中读取行时遇到问题。基本上,我的代码多读了一行,所有变量都为“0”,即 如果我的文件是: 0, 0, 3, 64, 0, 0, 0, 0 2, 0, 6, 64, 0, 0, 0, 0 4,
这个问题在这里已经有了答案: Why is “while( !feof(file) )” always wrong? (5 个答案) 关闭 5 年前。 所以我阅读了多篇关于为什么 feof 不能正常
最近几天我一直在处理一个奇怪的 PHP 问题,feof() 函数在文件结束之前返回 true。下面是我的代码的框架: $this->fh = fopen("bigfile.txt", "r");
我打开一个文件,在指针ptr 的地址找到流。我正在尝试查看文件是否为空白。使用以下内容 if (fgetc(ptr) != EOF) 按预期工作。当文件为空时,不执行该语句。当文件不为空时,语句不执行
我是一名优秀的程序员,十分优秀!