C程序从键盘读取数据并将其写入文本文件

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-29 03:04   11   0

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
FILE *fp;
char ch,file[10];
clrscr();
printf(“Enter file name:”);
scanf(“%s”,file);

fp=fopen(file,”w”); //file opening
if(fp==NULL) //exit program if file doesn’t open
{
printf(“File could not open!!”);
exit(0);
}

printf(“Enter data(* to exit)n”);
while(1)
{
ch=getche();
if(ch==’*’) //exit when * is pressed
exit(0);
putc(ch,fp);

}

fclose(fp); //file closing
}

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
FILE *fp;
char ch,file[10];
clrscr();
printf(“Enter file name:”);
scanf(“%s”,file);

fp=fopen(file,”w”); //file opening
if(fp==NULL) //exit program if file doesn’t open
{
printf(“File could not open!!”);
exit(0);
}

printf(“Enter data(* to exit)n”);
while(1)
{
ch=getche();
if(ch==’*’) //exit when * is pressed
exit(0);
putc(ch,fp);

}

fclose(fp); //file closing
}

翻译自: https://www.thecrazyprogrammer.com/2013/05/c-program-to-read-data-from-keyboard.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP