導航:首頁 > 文件教程 > c保存txt文件

c保存txt文件

發布時間:2024-10-17 15:32:06

⑴ c語言保存數據到文件txt

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

typedefstructnode
{
charname[20];
intnumber;
intprice;
intinventory;
intbrand;
structnode*next;
}N1;//這裡面類型,請根據你要的來定,我這只是給你參考
voidsave_data(FILE*ph,N1*h);
intmain(){
.....
.....//鏈表創建與錄入數據相關我就不寫了
FILE*fspointer;
fspointer=fopen("xxxxx.txt","w+");//第一個xxxxx是你文件的名字,自己起
save_data(fspointer,y);//假設y是你鏈表的頭結點
....
....
}

voidsave_data(FILE*ph,N1*h)
{
N1*hed=h;
fprintf(ph,"name number price inventory brand ");
while(hed->next!=NULL)
{
hed=hed->next;
fprintf(ph,"%s %d %d %d %d ",hed->name,hed->number,hed->price,hed->inventory,hed->brand);
}
fclose(ph);
}

⑵ 如何將在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語言如何保存字元串為txt文件

使用來fopen() fwrite() 等函數,詳細源可以參考c/c++標准庫。下面是一個示例代碼,向1.txt中寫入20位元組的字串。

#include <stdio.h>
int main()
{
char str[20] = "testtesttesttesttes";
FILE *fp = fopen("1.txt", "w+");
if (fp==0) {
printf("can't open file\n");
return 0;
}
fwrite(str, sizeof(char)*20, 1, fp);
fclose(fp);
return 0;
}

⑷ C程序運行後的數據,如何將其保存到TXT文本里

將數據保存到文本中其實就是將數據寫入到txt文件中,步驟如下

  1. 打開一個文件獲取文件句柄:fopen("text.txt","w+");//打開一個文件,如果該文件不存在創建該文件

  2. 使用fputs(),fwrite()等函數將相應的數據寫入文件

  3. 關閉文件fclose(fd);

#include<stdio.h>
main()
{
FILE *fp;
int i=0;
char *s="Am I right?";
fp=fopen("c://text.txt","w+");
while(*s)
{ printf("%c",*s);
fseek(fp,i++,SEEK_SET);
fprintf(fp,"%c",*s++); //++優先順序高於*
}
fclose(fp);
getchar();
}

⑸ C語言中,把數據儲存在txt文件中的代碼怎麼寫的

1、在vscode裡面添加了Python文件和用於讀取的文本文件。

閱讀全文

與c保存txt文件相關的資料

熱點內容
win10sd卡無法讀取 瀏覽:211
蘋果手機被偷了怎麼才能找回來 瀏覽:643
linux掃描網段ip 瀏覽:733
新舊改造文件名 瀏覽:114
激光下料軟體用的是NC文件 瀏覽:711
以什麼年齡來學c編程 瀏覽:938
網路素雞什麼意思 瀏覽:492
賺微信紅包的紙牌游戲 瀏覽:699
刪除win10主板密鑰 瀏覽:191
linux刪除最近文件 瀏覽:762
js字元串轉移字元串 瀏覽:191
怎麼用循環把數據錄入list 瀏覽:925
愛奇藝錯誤代碼104解決 瀏覽:303
三菱編程用什麼語言好 瀏覽:298
電子琴編程干什麼的 瀏覽:989
xar有什麼文件 瀏覽:485
sogou輸入法官方下載linux 瀏覽:916
word文件如何轉換成excel並保存 瀏覽:692
提取所有excel文件名 瀏覽:416
魅魔商店安卓版 瀏覽:157

友情鏈接