C语言_将字符串写入文本文件5
发布网友
发布时间:2023-10-20 02:52
我来回答
共3个回答
热心网友
时间:2024-12-03 21:07
从键盘输入一行字符,写入一个文件, 再把该文件内容读出显示在屏幕上。
#include<stdio.h>
main()
{
FILE *fp;
char ch;
if((fp=fopen("string","wt+"))==NULL)
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
printf("input a string:\n");
ch=getchar();
while (ch!='\n')
{
fputc(ch,fp);
ch=getchar();
}
rewind(fp);
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
printf("\n");
fclose(fp);
}
热心网友
时间:2024-12-03 21:07
实现代码如下:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
char *filePath = "文件位置";
char ch;
FILE *fp;
while ((fp = fopen(filePath, "w")) == NULL) {
printf("无法打开文件\n");
exit(0);
}
ch = getchar();
printf("请输入准备存储到文件的字符串\n");
ch = getchar();
while (ch != '#') {
fputc(ch, fp); //写入文件
putchar(ch); //输出到屏幕
ch = getchar();
}
fclose(fp);
putchar(10);
return EXIT_SUCCESS;
}
热心网友
时间:2024-12-03 21:07
FILE *fp;
char ch;fp=fopen("C:\\Documents and Settings\\Administrator\\桌面\\love_note.txt","w+");
if(fp == NULL)
{
printf("Cannot open file strike any key exit!");
exit(1);
}
while(1)
{
scanf("%c",&ch);
if(ch =='#') break;
fprintf(fp,"%c",ch);
}fclose(fp);
}