我是C编程新手,我试图创建一个程序来读取名为input的文件的上下文。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
FILE *in;
in = fopen("input","r");
printf("The contents of the file are\n");
fscanf(in,"%c",&ch);
printf("%c",ch);
fclose(in);
return 0;
}
答案 0 :(得分:1)
您的代码只读取文件的第一个字符。没有循环来读取整个文件。那是你的意图吗?
另外,检查文件是否成功打开。是输入文件名&#34;输入&#34; ?
答案 1 :(得分:0)
试试这个 -
java.util.Date objDt1 = getDate1FromSrc1(); // I am obtaining it from src1
java.util.Date objDt2 = getDate2FromOtherSrc(); // I am getting dt2 by other way.
答案 2 :(得分:0)
假设文件input
的内容为:
Hello World
您可以尝试以下代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
FILE *in;
in = fopen("input","r");
printf("The contents of the file are\n");
while(fscanf(in, "%c", &ch) != EOF)
{
printf("%c",ch);
}
fclose(in);
return 0;
}
<强>输出:强>
The contents of the file are
Hello World
答案 3 :(得分:-1)
你应该用这个:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
FILE *in;
/*Don't forget the extension (.txt)*/
if(in = fopen("input.txt","r") == NULL);
{
printf("File could not be opened\n");
}
else
{
printf("The contents of the file are\n");
/*I assume that you are reading char types*/
fscanf(in,"%c",&ch);
/*Check end-of-file indicator*/
while(!feof(in))
{
printf("%c",ch);
fscanf(in,"%c",&ch);
}
}
fclose(in);
return 0;
}
你应该记住验证文件是否开放,这总是一个好习惯。