c语言写入指定的文件
1.
要操作指定的文件,首先需要获得文件的文件描述符(句柄):fd。
=
fopen("test.txt "," w ")
2.
使用fprintf()或fputs()函数格式化数据并将其写入文本。
#包括
主()
{
文件* f;
f=fopen("wenzhang.txt "," w ");
fprintf(f,“这是一个c程序!”);
fclose(f);
}
如何用C语言将数据写入自定义文件 文件* f;定义文件指针
f = fopen("sub_data.txt "," w ");//打开文件
fprintf(……);//定义写什么。
fflush(f);//刷新
fclose(f);//关闭
如何用C语言将一个已有的字符串写入文件? 让要写入的数字的类型为int,然后使用控制字符串%d和%s来完成它。例如,代码行如下:
fprintf(fp," %d %s\n ",12345," abcdefg ");
其中:fp是成功写入打开文件的指针。这行代码先把整数12345写到文件里,然后加一个空格,再写字符串abcdefg,再写' \n '。
#包含“stdio.h”
#包含" string.h "
void main()
{
如何编写char aC语言文件? c的文本文件编写
// outfile.cpp -写入文件
#包括iostream
#包含文件I/O的fstream //文件
int main()
{
使用命名空间std
充电汽车[50];
int年;
双a _ price
双d _ price
ofstream outFile//为输出创建对象
outfile . open(" car info . txt ");//与文件相关联
cout "输入汽车的品牌和型号:";
cin.getline(汽车,50);
cout "输入年款:";
cin年;
cout "输入原始要价:";
CIN a _ price;
d _ price = 0.913 * a _ price
//用cout在屏幕上显示信息
cout固定;
cout . precision(2);
cout . setf(IOs _ base::show point);
cout“品牌和型号:”汽车endl
cout " Year:" Year endl;
cout "正在询问$ " a _ price endl
cout " Now asking $ " d _ price endl
//现在使用outFile代替cout做完全相同的事情
输出文件已修复;
outfile . precision(2);
outfile . setf(IOs _ base::show point);
“outFile”品牌和型号:“automobile endl
outFile“Year:”Year endl;
outFile "正在询问$ " a _ price endl
outFile " Now asking $ " d _ price endl;
outfile . close();//文件完成
返回0;
}
C语言如何将数据写入文件? 使用VC软件,可以通过写代码将数据写入文件。
先打开VC 6.0。
选择文件,新建。
选择C源文件创建一个新的空白文档。
首先声明头文件# includesdio.h。
编写主函数
空总管
主代码
FILE *infile、*outfile、* otherfile
char输入;
字符输入〔10〕;
int I = 0;
infile = fopen("d:\\infile.txt "," r ");//用fopen函数打开文件。
outfile = fopen("d:\\outfile.txt "," a ");//用fopen函数打开文件。
如果(!infile)
printf("打开infile失败....\ n ");
如果(!输出文件)
printf("打开输出文件失败...\ n ");
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
printf("**这个程序是为了显示文件操作!* * \ n ");
printf("**输入文件是:* \ n ");
printf(" * * d:\ \ infile . txt * * \ n ");
printf("**该文件中的内容是:* \ n ");
printf(" \ n ");
for(;;)
{
input = fgetc(infile);//无限循环读取文件内容。
printf("%c ",输入);
putc(输入,输出文件);//写内容
我;
if(input == '\n' || input == EOF)
打破;
}
fclose(infile);
fclose(outfile);
scanf("%d ",I)
运行结果
最后更新于 2023-10-07 19:31:11 并被添加「」标签,已有 位童鞋阅读过。
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处
相关文章