C语言 将文件指针传递给函数,并且文件未正确读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3847547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing file pointer into functions, and file not being read correctly
提问by John
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.
我认为我的代码有问题,文件没有被正确传递。输入是一个三行的文件 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);
}
回答by pmg
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"
这些声明对编译器说:“嘿编译器,请注意我将调用具有这些名称的函数(getNum、getLine、getMatrix、det1,...)并且它们返回int,但我没有告诉你它们是什么参数接受。当我使用它们时请相信我”
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 intand accept these parameters. If I make a mistake, do complain to let me know of my mistake"
这些声明对编译器说:“嘿编译器,请注意我将使用这些名称调用函数,它们返回int并接受这些参数。如果我犯了错误,请抱怨让我知道我的错误”
... continuing inside main()
... 在 main() 中继续
/* ... */
int lineSize;
int error;
getLine(line,lineSize,infile);
/* ... */
you declared lineSizebut 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.
您声明了lineSize但没有为变量提供值。当程序调用 getLine 时,lineSize 的值几乎肯定是错误的值(它甚至可能在调用该函数之前使您的计算机崩溃)。在使用它们之前初始化(几乎)所有变量。
/* ... */
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.
建议:提高编译器警告级别,不要在编译产生警告时运行程序。
回答by sth
In getLine(), when you give the infileFILE*to the getNum()function, you dereference it:
在getLine(),当你给infileFILE*的getNum()功能,你取消对它的引用:
l=getNum(value,*infile);
But getNum()would just expect a normal FILE*, not a dereferenced one. So pass infileto that function unchanged:
但getNum()只会期望一个正常的FILE*,而不是一个取消引用的。所以传递infile给那个函数不变:
l=getNum(value,infile);
Additionally, the while(value != '\n')loop will probably run forever, writing past the end of the linesarray 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 copyof the integer passed as a parameter and then modifies this copy. The original valueis never changed.
此外,while(value != '\n')循环可能会永远运行,写入超过lines数组的末尾,直到出现分段错误。value,它控制循环何时终止,永远不会被修改(它也没有被初始化,使其以任意值开始)。该getNum()函数可能应该修改value,获取作为参数传递的整数的副本,然后修改该副本。原作value永远不会改变。
If you want the function to change the valuevariable you have to use a pointer that points to valueand that is used to modify that variable:
如果您希望函数更改value变量,则必须使用指向value并用于修改该变量的指针:
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?
此外value,一个整数变量被分配并与'\n'一个字符文字进行比较也有点可疑。您确定要使用整数值'\n'作为循环的终止条件吗?
回答by RonLugge
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.
虽然不是直接的答案,但我建议在随机位置粘贴一些 printf 语句;这会让你相对快速地缩小崩溃的确切点。移动它们,直到您有两个 printf 括起一行代码,然后您就知道这是崩溃的罪魁祸首,这将使您更好地诊断。

