赛格故障读取文件时出错用于fgets [英] Seg Fault error reading from file with fgets

查看:138
本文介绍了赛格故障读取文件时出错用于fgets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//declare double pointer so that create array can "return" an array
int **aryReturn;
int size;
char trashdata[100];

//open file
FILE *inFilePtr = fopen(*(argv + 1), "r" );

if (inFilePtr != NULL)
    printf(" the value of argv 1 is %s \n", argv[1]); 

while (fgets(trashdata, sizeof(int) * 10, inFilePtr) != NULL){
    fgets(trashdata, 10, inFilePtr);
    size++;
}

谁能告诉我为什么我的循环条件都不行!我得到一个与fgets赛格故障,说FP(为0x0)。
我曾尝试

can anyone tell me why my loop condition will not work! I get a seg fault that says fp(0x0) at fgets. I have tried

while (!feof(inFilePtr))

和我基本上得到了同样的错误,但它说的feof的问题。

And I basically get the same error, but it says feof is the problem.

我的文件似乎正常打开,因为if语句打印..和argv所期待的文件名

My file seems to open correctly because the if statement prints.. and argv has the expected file name

推荐答案

好了,我看到了一些问题...

Well, I see a few problems...


  • 尺寸未初始化

  • 调用获得()连续两次似乎很奇怪,见的feof()

  • 您不是测试后 fopen()函数导致对空,所以第一个获得()可以弹了出来出于这个原因...你得到的消息?

  • size is not initialized
  • calling gets() twice in a row seems odd, see feof()
  • you aren't doing anything after testing the fopen() result against NULL, so the first gets() could bomb out for that reason ... do you get that message?

只有最后一件事似乎有可能抛出一个异常,但我的哲学是永远的解决已知问题并重新测试的...这是浪费时间,以错误的predict互动。

Only the last thing seems likely to throw an exception, but my philosophy is always fix the known problems and retest ... it's a waste of time to predict interactions between bugs.

这篇关于赛格故障读取文件时出错用于fgets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆