我对C有点新,但基本上我有一个问题,我需要从文件中读取'-1'。可悲的是,这意味着我遇到了文件的过早结尾,因为EOF常量在我的编译器中也是-1。C - 使用fscanf从文件中读取'-1'
这会有什么样的解决方法?是否有另一个函数可以用来读取它,将EOF更改为我可以使用的东西?
在此先感谢。
,因为人们都在问它
int read() {
int returnVal; // The value which we return
// Open the file if it isn't already opened
if (file == NULL) {
file = fopen(filename, "r");
}
// Read the number from the file
fscanf(file, "%i", &returnVal);
// Return this number
return returnVal;
}
这个数字是后来比较EOF的代码。
好吧,这可能是不好的做法,但我改变了代码以下
int readValue() {
int returnVal; // The value which we return
// Open the file if it isn't already opened
if (file == NULL) {
file = fopen(filename, "r");
}
// Read the number from the file
fscanf(file, "%i", &returnVal);
if (feof(file)) {
fclose(file);
return -1000;
}
// Return this number
return returnVal;
}
因为我知道我永远不会从我的文件中读出任何这样的数字(它们的范围从约[-300,300]感谢您的所有帮助家伙!
EOF常量是-1并不重要......应该永远不会检查您读取的值,并且因-1以外的任何原因返回为-1(或者什么都不是)被读取,你的记忆碰巧包含-1)。发布你认为你遇到问题的代码,你可以得到帮助,但没有这个问题,堆栈溢出标准是不完整的。 – mah