首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于fscanf().解决办法

2013-03-14 
关于fscanf()....#includestdio.hint main(){FILE *input,*outputint a,binput fopen(1.in,wb)

关于fscanf()....
#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中,为什么这个程序就不能运行呢?编译通过,但输入不了数据  input output file c
[解决办法]
 input = fopen("1.in","wb");
这里打开的模式不对:
w---打开一个空文件来编写。若文件已经存在,内容会被全部清空;若文件不在则创建。
把wb换成只读r+就可以了,因为你只是需要赌球1.in里面的东西
[解决办法]
input = fopen("1.in","wb");这句把"rb"误写为"wb"了。
[解决办法]
注意打开方式

#include<stdio.h>
 int main()
 {
   int a,b;
   FILE* in = fopen("1.in","rb"), *out = fopen("1.out","wb");
   if(f && 2 == fscanf(input,"%d%d",&a,&b) && out)
   {
     fprintf(output,"%d",a+b);
   }
   fclose(in), fclose(out);   
   return 0;
 }

[解决办法]
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. 
[解决办法]
打开的姿势不对 大家都说了 
大家没说的是 你根本没有输出到控制台的语句 控制台当然没有输出了啦
printf或者fprintf(stdout, ...) 呢?

热点排行