c语言中怎么向文件中写入数据啊 具体点 谢谢

如题所述

不知你向文件输入的是什么数据,输入数据的函数很多,有fputc(s,fp);有fwrite()函数、、、、
下面是想文件输入字符,并把字符串中的小写字符转换成大写字符:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int main()
{
FILE *fp;
char filename[20];
printf("请输入文件的名称:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cannot open file ,,,\n");
exit(0);

}
printf("请输入字符直至结束(ctrl +z):");
fflush(stdin);
char s;

while(scanf("%c",&s),=EOF)
{
if(islower(s))
s=toupper(s);//把小写字符转换成大写字符
fputc(s,fp);

}

rewind(fp);//是位置指针重新返回文件的开头,此函数没有返回值

if((fp=fopen(filename,"r"))==NULL)//以读的方式打开文件
{
printf("cannot open file ,,,\n");
exit(0);

}

while(,feof(fp))
{
s=getc(fp);
putchar(s);

}

return 0;

}
测试:
请输入文件的名称:hello
请输入字符直至结束(ctrl +z):hello world ,
Z
Z。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-09-11
用scanf函数或者getchar都可以的。scanf函数,与printf函数一样,都被定义在stdio.h里,因此在使用scanf函数时要加上#include<stdio.h>。它是格式输入函数,即按用户指定的格式从键盘上把数据输入到指定的变量之中,其关键字最末一个字母f即为“格式”(format)之意。scanf函数的一般形式
scanf(格式控制,地址表列) “格式控制”的含义同printf函数;“地址表列”是由若干个地址组成的表列,可以是变量的地址,或字符串的首地址。
第2个回答  推荐于2017-12-16
你好!!
不知你向文件输入的是什么数据,输入数据的函数很多,有fputc(s,fp);有fwrite()函数、、、、
下面是想文件输入字符,并把字符串中的小写字符转换成大写字符:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int main()
{
FILE *fp;
char filename[20];
printf("请输入文件的名称:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cannot open file !!!\n");
exit(0);

}
printf("请输入字符直至结束(ctrl +z):");
fflush(stdin);
char s;

while(scanf("%c",&s)!=EOF)
{
if(islower(s))
s=toupper(s);//把小写字符转换成大写字符
fputc(s,fp);

}

rewind(fp);//是位置指针重新返回文件的开头,此函数没有返回值

if((fp=fopen(filename,"r"))==NULL)//以读的方式打开文件
{
printf("cannot open file !!!\n");
exit(0);

}

while(!feof(fp))
{
s=getc(fp);
putchar(s);

}

return 0;

}
测试:
请输入文件的名称:hello
请输入字符直至结束(ctrl +z):hello world !
Z
Z
HELLO WORLD !
�Press any key to continue本回答被提问者采纳
相似回答