#include <stdio.h>
int openfile () //opens a file and saves the content in c
{
int c;
FILE *file;
file = fopen("File", "r"); /* looks for a File named "File" */
if (file == NULL)
{
printf("Error\n");
}
while(1) {
c = fgetc(file);
if(feof(file)) {
break;
}
printf("%c", c); /* This prints the contents of the file */
}
fclose(file);
return c;
}
int otherfunction1337() /* <- Problem: Why doesn't this add 10 and the
content of the file (in my example I wrote 20 into the file) */
{
int a = 10 + openfile(); /* ? */
printf("%i", a);
return 0;
}
int main (void)
{
otherfunction1337();
}
这就是我到目前为止所得到的。请记住,我使用名为“File”的文本文件对此进行了测试。我只是在里面写了“20”。我预计它会输出 20 + 10 (30) 的结果,但它不起作用。
该openfile
函数总是返回 0 并且只在控制台上打印文件的内容。您应该将文件的内容转换为数字并返回该值。
您可以使用:
int c; fscanf(file, "%d", &c); return c;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句