自己按要求写的C语言的题,有错误请大家看下

来源:百度知道 编辑:UC知道 时间:2024/05/04 14:44:30
若输入string 12,下列程序输出结果为_______

#include<stdio.h>

main()

{char s[80];

int a;

FILE *fp;

if ((fp=fopen("test","w"))==NULL)

{puts("cannot open the file");

exit();

}

fscanf(stdin,"%s%d",s,&a);

fprintf(fp,"%s%d",s,a);

fclose(fp);

if((fp=fopen("test","t”))==NULL)

{puts("cannot open the file");

exit();

}

fscanf(fp,"%s%d",s,&a);

fprintf(stdout,"%s%d\n,s,a);

fclose(fp);

}
请问上面运行后的结果是什么?
好象有错误
给找下吧
谢谢了
顺便说下过程

不知道你的要求是什么
这个程序编译就通不过,有两个地方笔误:
if((fp=fopen("test","t”))==NULL)
这一句有个引号用了全角

fprintf(stdout,"%s%d\n,s,a);
这一句漏了个引号

还有就是exit用得不对,要用exit的话需要
#include <stdlib.h>
而且exit要带参数,随便写个几都行,就是不能空着

运行的结果就是:
cannot open the file

过程:
if ((fp=fopen("test","w"))==NULL)
这一句一般是可以的,最多新建一个名叫test的新文件
假如已经有了叫test的目录,那就根本用不着输入了,到这里就直接输出cannot open the file了,程序退出

假如上一次打开文件成功,那么可以按照你说的,输入string 12,分别赋值给s和a

程序最远只能跑到这里,然后从这里的exit退出
if((fp=fopen("test","t”))==NULL)
{puts("cannot open the file");
exit();
}

因为fopen这个函数根本没有"t"这个模式
你可以通过调试跟踪进去看,这样是打不开文件的
http://www.gznc.edu.cn/yxsz/jjglxy/book/php4gb/function.php-fopen.htm