最简单的文件操作。。
#include<stdio.h>
int main()
{
FILE *input,*output;
int a,b;
input = fopen("1.in","wb");
fscanf(input,"%d%d",&a,&b);
fclose(input);
output = fopen("1.out","wb");
fprintf(output,"%d",a+b);
return 0;
}
将输入保存在1.in文件中,输出保留在1.out中,为什么这个程序就不能运行呢?编译通过,但输入不了数据 c 文件操作 fopen FILE fscanf
[解决办法]
int fscanf(FILE *stream, const char *format, ...)
fscanf reads from stream under control of format, and assigns converted values through subsequent arguments, each of which must be a pointer. It returns when format is exhausted. fscanf returns EOF if end of file or an error occurs before any conversion; otherwise it returns the number of input items converted and assigned.
int fprintf(FILE *stream, const char *format, ...)
fprintf converts and writes output to stream under the control of format.
[解决办法]
google, 看看别人如何用
[解决办法]
input = fopen("1.in","wb");
打开文件模式不对,wb改成rb或者r+
[解决办法]
input = fopen("1.in","rb");
最简单的程序也告诉我们,错一个字符就得不到我们像要的结果。
[解决办法]
楼主下面是我运行你的代码有地方进行了修改:
在VS2010环境测试通过。
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
FILE *input,*output;
int a,b;
input = fopen("E:\in.txt","r");//路径有改变,其实这个不影响程序的,你懂得,例外文件打开方式修改了。
fscanf(input,"%d,%d",&a,&b);
output = fopen("E:\out.txt","w");//路径有改变,其实这个不影响程序的,你懂得,例外文件打开方式修改了。
fprintf(output,"%d",a+b);
fclose(output);
fclose(input);
return 0;
}
其实你需要理解fscanf函数的用法:它是从流文件中输入信息,就是说是从文件中读取信息。
注:fscanf和scanf的区别:前者是从流文件中输入相关信息
fprintf和printf的区别:前者是将信息打印到文件流中,后者则是将信息打印到控制台中。
希望对您有所帮助,现在程序没有问题了。