⑴ c語言把數據保存在TXT文本中
c語言,把數據存在txt文件里,需要使用fopen函數以寫文件的方式打開文件。
然後可以使用fprintf,fputc,fputs,fwrite等函數,把相應類型的數據寫入文件。
最後,寫入完成後使用fclose函數關閉文件。
下面的C語言程序源程序展示了合並A.txt和B.txt兩個TXT文件的內容存儲到到新建的一個TXT文件,C.txt。
#include<stdio.h>
#include<stdlib.h>
usingnamespacestd;
typedefstructStudent{
charname[32];
charsex[6];
intage;
floatscore;
}stu;
intmain(intargc,char*argv[]){
stua[48];
FILE*ra=fopen("A.txt","r");
FILE*rb=fopen("B.txt","r");
FILE*wc=fopen("C.txt","w");
if(ra==NULL||rb==NULL||wc==NULL){
printf("failedtoopenfile ");
system("pause");
return0;
}
inti=0;
while(fscanf(ra,"%s%s%d%f",&a[i].name,&a[i].sex,&a[i].age,&a[i].score)!=EOF){
i++;
}
fclose(ra);
while(fscanf(rb,"%s%s%d%f",&a[i].name,&a[i].sex,&a[i].age,&a[i].score)!=EOF){
i++;
}
fclose(rb);
intn=i;
for(i=0;i<n;i++){
fprintf(stdout,"%s %s %d %g ",a[i].name,a[i].sex,a[i].age,a[i].score);
fprintf(wc,"%s %s %d %g ",a[i].name,a[i].sex,a[i].age,a[i].score);
}
fclose(wc);
system("pause");
return0;
}
其中A.txt中的內容如下:
⑵ C語言如何把結果保存在txt中
方法和詳細的操作步驟如下:
1、第一步,添加了Python文件和文本文件,可在vscode中讀取,見下圖,轉到下面的步驟。
⑶ C語言輸出的東西怎麼保存到文本文件中
先用fopen打開一個文件,然後用它返回的句柄就可以操作文件了.字數限制,未給出函數原型,請網路
⑷ 如何將C語言運行的結果存到TXT文本中
簡單,直接用管道的方式,比如你的程序為a.exe
那麼直接, ./a.exe > 1.txt 直接寫就入 1.txt中了
⑸ 怎樣把C語言生成的數據保存在文本中
使用fprintf()函數,按照需要的格式保存數據。例如
#include<stdio.h>
void
main()
{
int
i,a[10];
FILE
*fp;
if((fp=fopen("test.txt","wt"))==NULL)
{
printf("cannot
open
file\n");
return;
}
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
fprintf(fp,"%d\n",a[i]);//保存數組元素
}
fclose(fp);
}
⑹ 怎麼把c語言編的程序的結果輸入到一個文本文件中
c語言編的程序的結果輸入到一個文本文件中可以使用fprintf;
例:
#include<stdio.h>
main(){
FILE *fpt;
fpt = fopen("wendangming.txt","w");//打開文檔,寫入
fprintf(fpt,"Hello world");
fclose(fpt);
}
(6)c語言怎麼保存到文本上擴展閱讀
它打開一個文本文件,逐個字元地讀取該文件
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream testByCharFile;
int num;
char c;
testByCharFile.open("6.5.cpp",ios::in);
while(!testByCharFile.eof())
{
testByCharFile >> c;
num++;
}
testByCharFile.close();
cout << num << endl;
}
⑺ 用C語言如何將結果輸出到一個文本文件中保存
文件的操作步驟:
#include <stdio.h> #include <stdlib.h> int main()
{
FILE *fp;
int i, d;
fp=fopen("data.txt","w");
if(fp==NULL)
{
printf("File cannot open! " );
exit(0);
}
for(i=0; i<10; i++)
{
scanf("%d", &d);
fprintf(fp,"%d ", d);
}
fclose(fp);
return 0;
}
格式化輸出:
#include <stdio.h> #include <stdlib.h> int main()
{
FILE *fp;
int i, No;
float salary;
fp=fopen("data.csv","w");
if(fp==NULL)
{
printf("File cannot open! " );
exit(0);
}
//輸入5名員工的工號,並保存到文件中
for(i=0; i<5; i++)
{
scanf("%d %f", &No, &salary);
fprintf(fp,"%d, %.2f ", No, salary);
}
fclose(fp);
return 0;
}
(7)c語言怎麼保存到文本上擴展閱讀:
從文件中讀取字元:
#include <stdio.h> #include <stdlib.h> int main()
{
FILE *fp;
char c;
if ((fp=fopen( "data.dat" , "r" ))==NULL)
{
printf("File cannot open!");
exit(0);
}
while((c=fgetc(fp))!=EOF)
putchar(c);
fclose(fp);
return 0;
}
⑻ C語言如何寫入文本文件
1、首先輸入下方的代碼
#include <stdio.h>
int main()
{
//下面是寫數據,將數字0~9寫入到data.txt文件中
FILE *fpWrite=fopen("data.txt","w");
if(fpWrite==NULL)
{
return 0;
}
for(int i=0;i<10;i++)
fprintf(fpWrite,"%d ",i);
fclose(fpWrite);
//下面是讀數據,將讀到的數據存到數組a[10]中,並且列印到控制台上
int a[10]={0};
FILE *fpRead=fopen("data.txt","r");
if(fpRead==NULL)
{
return 0;
}
for(int i=0;i<10;i++)
{
fscanf(fpRead,"%d ",&a[i]);
printf("%d ",a[i]);
}
getchar();//等待
return 1;
}
⑼ 如何將在c語言中生成的數據保存到文本文件中
主要通過fprintf格式化輸出函數實現,主要代碼如下,
//程序功能,將10 12.345000 testinfo 寫入test.txt文件
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *pf=NULL;
int m=10;
float f=12.345;
char str[20]="testinfo";
pf=fopen("test.txt", "w" );//假設test.txt文件為空
if(!pf)
{
printf("打開文件失敗,程序退出!");
exit(1);
}
fprintf(pf,"%d %f %s\n",m,f,str);//寫入,test.txt文件內容為10 12.345000 testinfo
if(pf)//關閉文件
{
fclose( pf);
pf=NULL;
}
printf("數據已寫入test.txt文件!\n");
return 0;
}
int fprintf( FILE *stream, const char *format, ... );fprintf()函數根據指定的format(格式)發送參數到由stream指定的文件。fprintf()只能和printf()一樣工作,fprintf()的返回值是輸出的字元數,發生錯誤時返回一個負值。
⑽ c語言,如何將此程序產生的數據保存到文本文件中
//獲取文件指針
FILE *pFile = fopen("1.txt", //打開文件的名稱
"w"); // 文件打開方式 如果原來有內容也會銷毀
//向文件寫數據
fwrite (str,//要輸入的文字
1,//文字每一項的大小 以為這里是字元型的 就設置為1 如果是漢字就設置為4
strlog(str), //單元個數 我們也可以直接寫5
pFile //我們剛剛獲得到的地址
);
fclose(pFile); //關閉保存