将文件指针传递给函数,文件无法正确读取

发布于 2024-09-25 16:57:21 字数 2044 浏览 0 评论 0原文

我认为我的代码存在问题,文件未正确传递。输入是一个包含三行的文件 1 2 3; 4 5 6; 7 8 9; 并且输出是分段错误(核心转储),输出应该打印第一行 1 2 3。

#include <stdio.h>
#include <stdlib.h>

int getNum();
int getLine();
int getMatrix();
int det1();
int det2();
int det3();
int det4();
int det5();
int det6();


main(){
  FILE *infile;
    infile = fopen("matrix.txt","r");
  int line[6];
  int lineSize;
  int error;
  getLine(line,lineSize,infile);
  printf("%d %d\n", line[0],line[1]);
  fclose(infile);
}

/***********************************************
Name : getLine
Description : To get the line of numbers
Arguments :  infile - the file pointer with numbers inside
             line[]  - the line of numbers
             lineSize - size of line
Returns : 1   - If no errors were encountered
          2 - If END OF FILE was reached
          -1 if non number detected

*************************************************/
int getLine(int line[], int lineSize, FILE *infile){
  int value;
  int l;
  lineSize=0;

  while(value != '\n'){
    value=0;
    l=getNum(value,*infile);
    if (value==EOF){
      return(2);
    }
    line[lineSize]=value;
    lineSize++;
  }
  if (l == -1){
    return(-1);
  }
  return(1);

}


/***********************************************
Name : getNum
Description : To get the Next number from file
Arguments :  infile - the file with numbers inside
             value  - the value of number grabed
Returns : 1   - If no errors were encountered
          -1  - If letter or non number detected
*************************************************/
int getNum(int value, FILE *infile){

  int c;
  int error=1;

  while ((c=getc(infile)) != EOF){
    if (c=='\n'){
      value = '\n';
      return(1);
    }
    if(c==32){//checking for space
      if (error == -1){
        return(-1);
      }
      else{
        return(1);
      }
    }
    else {
      value = 10*value + c - '0';
    }
    if((c<=47)||(c>=58)){
      printf("incorrect number input %d\n",c);
      error = -1;
    }
  }
  value = EOF;
  return(1);

}

I think my problem with my code that the file is not being passed correctly. The input is a file with three lines
1 2 3;
4 5 6;
7 8 9;
and the output is a Segmentation fault (core dumped), the output is supposed to print the first line 1 2 3.

#include <stdio.h>
#include <stdlib.h>

int getNum();
int getLine();
int getMatrix();
int det1();
int det2();
int det3();
int det4();
int det5();
int det6();


main(){
  FILE *infile;
    infile = fopen("matrix.txt","r");
  int line[6];
  int lineSize;
  int error;
  getLine(line,lineSize,infile);
  printf("%d %d\n", line[0],line[1]);
  fclose(infile);
}

/***********************************************
Name : getLine
Description : To get the line of numbers
Arguments :  infile - the file pointer with numbers inside
             line[]  - the line of numbers
             lineSize - size of line
Returns : 1   - If no errors were encountered
          2 - If END OF FILE was reached
          -1 if non number detected

*************************************************/
int getLine(int line[], int lineSize, FILE *infile){
  int value;
  int l;
  lineSize=0;

  while(value != '\n'){
    value=0;
    l=getNum(value,*infile);
    if (value==EOF){
      return(2);
    }
    line[lineSize]=value;
    lineSize++;
  }
  if (l == -1){
    return(-1);
  }
  return(1);

}


/***********************************************
Name : getNum
Description : To get the Next number from file
Arguments :  infile - the file with numbers inside
             value  - the value of number grabed
Returns : 1   - If no errors were encountered
          -1  - If letter or non number detected
*************************************************/
int getNum(int value, FILE *infile){

  int c;
  int error=1;

  while ((c=getc(infile)) != EOF){
    if (c=='\n'){
      value = '\n';
      return(1);
    }
    if(c==32){//checking for space
      if (error == -1){
        return(-1);
      }
      else{
        return(1);
      }
    }
    else {
      value = 10*value + c - '0';
    }
    if((c<=47)||(c>=58)){
      printf("incorrect number input %d\n",c);
      error = -1;
    }
  }
  value = EOF;
  return(1);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笛声青案梦长安 2024-10-02 16:57:21

浏览你的代码......

int getNum();
int getLine();
int getMatrix();
int det1();
/* ... */

这些声明对编译器说:“嘿编译器,请注意我将使用这些名称(getNum,getLine,getMatrix,det1,...)调用函数,并且它们返回 int< /code>,但我不会告诉你它们接受什么参数,当我使用它们时请相信我”

如果你在向编译器引入函数时正确使用原型,那就更好了

int getNum(int value, FILE *infile);
int getLine(int line[], int lineSize, FILE *infile);
/* ... */

这些声明对编译器说:“嘿编译器” ,请注意,我将使用这些名称调用函数,它们返回 int 并接受这些参数,如果我犯了错误,请抱怨让我知道我的错误”

...继续里面。 main()

      /* ... */
      int lineSize;
      int error;
      getLine(line,lineSize,infile);
      /* ... */

声明了 lineSize 但没有为变量提供值。当程序调用 getLine 时,lineSize 的值几乎肯定是错误的值(甚至可能在调用该函数之前使您的计算机崩溃)。在使用(几乎)所有变量之前初始化它们。

      /* ... */
      int lineSize = 0;
      int error = 0;
      getLine(line,lineSize,infile);
      /* ... */

我没有浏览更多...

建议:提高编译器警告级别,并且在编译产生警告时不要运行程序。

Skimming your code ...

int getNum();
int getLine();
int getMatrix();
int det1();
/* ... */

These declarations say to the compiler: "hey compiler, please be aware I'll be calling functions with these names (getNum, getLine, getMatrix, det1, ...) and they return int, but I'm not telling you what parameters they accept. Just trust me when I use them"

It's better if you use the prototype right when you introduce the functions to the compiler

int getNum(int value, FILE *infile);
int getLine(int line[], int lineSize, FILE *infile);
/* ... */

These declarations say to the compiler: "hey compiler, please be aware I'll ba calling function with these names, they return int and accept these parameters. If I make a mistake, do complain to let me know of my mistake"

... continuing inside main()

      /* ... */
      int lineSize;
      int error;
      getLine(line,lineSize,infile);
      /* ... */

you declared lineSize but didn't provide a value for the variable. When the program calls getLine, the value for lineSize is almost certainly the wrong value (it might even make your computer crash even before calling the function). Initialize (almost) all variables before using them.

      /* ... */
      int lineSize = 0;
      int error = 0;
      getLine(line,lineSize,infile);
      /* ... */

I haven't skimmed more ...

Suggestion: crank up your compiler warning level and do not run your program while compilation produces warnings.

难以启齿的温柔 2024-10-02 16:57:21

getLine() 中,当您将 infile FILE* 赋予 getNum() 函数时,您取消引用它:

 l=getNum(value,*infile);

但是 getNum() 只期望一个正常的 FILE*,而不是取消引用的文件。因此,将 infile 不变地传递给该函数:

 l=getNum(value,infile);

此外,while(value != '\n') 循环可能会永远运行,写入超过 的末尾lines 数组,直到出现分段错误。 value 控制循环何时终止,它永远不会被修改(它也不会被初始化,使其以任意值开始)。 getNum() 函数可能应该修改 value,它获取作为参数传递的整数的副本,然后修改此副本。原始永远不会改变。

如果您希望函数更改 value 变量,则必须使用指向 value 的指针,该指针用于修改该变量:

int getNum(int *value, ...) {
   *value = 5;
   ...
}

l=getNum(&value, infile);

此外,还有一点值得怀疑: value(一个整型变量)被赋值并与 '\n'(一个字符文字)进行比较。您确定要使用 '\n' 的整数值作为循环的终止条件吗?

In getLine(), when you give the infile FILE* to the getNum() function, you dereference it:

 l=getNum(value,*infile);

But getNum() would just expect a normal FILE*, not a dereferenced one. So pass infile to that function unchanged:

 l=getNum(value,infile);

Additionally, the while(value != '\n') loop will probably run forever, writing past the end of the lines array until you get a segmentation fault. value, which is controlling when the loop will terminate, is never modified (also it isn't initialized, making it start with an arbitrary value). The getNum() function, which probably is supposed to modify value, gets a copy of the integer passed as a parameter and then modifies this copy. The original value is never changed.

If you want the function to change the value variable you have to use a pointer that points to value and that is used to modify that variable:

int getNum(int *value, ...) {
   *value = 5;
   ...
}

l=getNum(&value, infile);

Also it is a little dubious that value, an integer variable, is assigned and compared against '\n', a character literal. Are you sure you want to use the integer value of '\n' as a termination condition of your loop?

半夏半凉 2024-10-02 16:57:21

虽然不是直接答案,但我建议在随机位置粘贴一些 printf 语句;这将使您相对快速地缩小崩溃的确切点。移动它们,直到有两个 printfs 将一行代码括起来,然后您就知道该代码是崩溃的罪魁祸首,这将让您更好地诊断。

While not a direct answer, I'd recommend sticking a number of printf statements in at random spots; that will let you narrow down the exact point of the crash relatively quickly. Move them around until you have two printfs bracketing a single line of code that you then know to be the crashing culprit, which will let you diagnose better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文