//_59_fprintf()和fscanf()
//_59_main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
int main()
{
FILE *fp;
char str[80],str1[80];
int i,j;
if((fp=fopen("text.txt","w"))==NULL)//以写入的方式打开
{
printf("Can not open the file.\n");
exit(0);
}
printf("Please enter a string and a number:\n");
fscanf(stdin,"%s %d",str,&i);//参数stdin表示从键盘读入
fprintf(fp,"%s %d",str,i);
fclose(fp);
if((fp=fopen("text.txt","r"))==NULL)//以读的方式打开
{
printf("Can not open the file.\n");
exit(0);
}
fscanf(fp,"%s %d",str1,&j);
fprintf(stdout,"%s %d\n",str1,j);//参数strout表示写向屏幕
fclose(fp);
system("pause");
return 0;
}59_fprintf()和fscanf()
最新推荐文章于 2024-04-15 07:37:43 发布
本文介绍了一个简单的C++程序,演示了如何使用fopen、fscanf和fprintf函数来读取和写入文本文件,并将文件内容输出到控制台。程序首先提示用户输入字符串和整数,然后将这些数据写入文件。之后,程序从同一文件中读取数据,并将读取的内容输出到控制台。
5406

被折叠的 条评论
为什么被折叠?



