需要帮助解决 C 中文件 i/o 的分段错误
我读取了有 911 行的输入文件,并希望在对每一行进行一些更改后将这 911 行复制到输出文件中。
当我运行此文件时,我遇到了分段错误。我不知道为什么。谁能帮忙..
#include<stdio.h>
void main()
{
int i;
FILE *fin,*fop;char* str;
fin=fopen("atk561011.txt","r");
if(fin=NULL) printf("ip err");
fop=fopen("svmip.txt","w");
if(fop=NULL) printf("op err");
for(i=1;i<=911;i++)
{
fgets(str,150,fin);
if((i>300&&i<=360)||(i>600&&i<=660))
str[7]='1';
else
str[7]='0';
fputs(str+7,fop);
putc('\n',fop);
}
fclose(fin);
fclose(fop);
}
I read the input file , which has 911 lines , and want to copy these 911 lines into the output file , after making a few changes to each line..
I am getting segmentation fault when i run this.. I dont know why.. can anyone please help..
#include<stdio.h>
void main()
{
int i;
FILE *fin,*fop;char* str;
fin=fopen("atk561011.txt","r");
if(fin=NULL) printf("ip err");
fop=fopen("svmip.txt","w");
if(fop=NULL) printf("op err");
for(i=1;i<=911;i++)
{
fgets(str,150,fin);
if((i>300&&i<=360)||(i>600&&i<=660))
str[7]='1';
else
str[7]='0';
fputs(str+7,fop);
putc('\n',fop);
}
fclose(fin);
fclose(fop);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,这是错误的:
应该是:(
当然,
fop
也是如此)。如果您没有成功打开文件 - 不要只是打印错误,退出,因为您要读取什么?请记住,printf
的输出是缓冲的,如果出现段错误,您根本不会看到它,即使它在故障之前运行。另一件事:您不为
str
分配内存,而是使用fgets
写入它。另一件事:从文件中读取预定义数量的行可能是一个坏主意。最好从输入中读取,直到文件结尾,或者直到读取了所需的行数。
For a start, this is wrong:
Should be:
(the same goes for
fop
, of course). And if you didn't succeed opening the file - don't just print an error, exit, because what are you going to read from? Keep in mind that the output ofprintf
is buffered and in case of a segfault you won't always see it at all, even if it ran before the fault.Another thing: you don't allocate memory for
str
, yet write into it withfgets
.And another thing: reading a pre-defined amount of lines from the file is probably a bad idea. It's better to read from the input until there is an end of file, or until a required amount of lines has been read.
您没有为指针 str 分配任何空间。
将其更改为 char str[/*Max Length 您期望 150? */] 或分配缓冲区。
与此同时,您的代码正在遍历内存 - 因此出现分段错误。
Your not allocating any space for the pointer str.
Change that to either char str[/*Max Length You expect 150? */] or allocate the buffer.
In the meantime your code is walking all over memory - thus the segmentation fault.
这是应该执行此操作的更正代码。
我添加了逻辑检查以确保文件是否存在,以继续处理。如果不这样做将会导致代码崩溃。因为在原始代码中,如果输入失败或输出失败,您将打印“ip err”,但继续进入
for
循环,在这种情况下,这将导致执行失败,如下所示失败时它仍然会尝试从不存在的文件句柄中读取。编辑:请参阅代码中的上述注释。您是否尝试根据 300-360 和 600-660 范围内的
i
条件值将 1/0 输出到输出文件中。你能澄清一下吗? 希望这会有所帮助,
此致,
汤姆.
Here's the corrected code that should do it.
I have added logic checking to ensure if the file exists, to continue processing. Failure to do so will cause the code to blow up. Since in the original code, you were printing "ip err" if the input failed or in the case of output failure, yet continue on into the
for
loop which will render the execution to fail in that case as it would still be attempting to read from an non-existant file handle on failure.Edit: Please see the comment above in the code. Are you trying to output 1/0's based on the conditional value of
i
between the ranges of 300-360 and 600-660 inclusively, into the output file. Can you clarify? Should that beHope this helps,
Best regards,
Tom.
fin=NULL 和 fop=NULL 都应该使用“等于”运算符。您将 fin 和 fop 设置为 NULL,而不是检查错误的返回值。
Both fin=NULL and fop=NULL should both be using the 'equal-equal' operator. You're setting fin and fop to NULL instead of checking for a bad return value.