導航:首頁 > 文件教程 > 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文件相關的資料

熱點內容
maya粒子表達式教程 瀏覽:84
抖音小視頻如何掛app 瀏覽:283
cad怎麼設置替補文件 瀏覽:790
win10啟動文件是空的 瀏覽:397
jk網站有哪些 瀏覽:134
學編程和3d哪個更好 瀏覽:932
win10移動硬碟文件無法打開 瀏覽:385
文件名是亂碼還刪不掉 瀏覽:643
蘋果鍵盤怎麼打開任務管理器 瀏覽:437
手機桌面文件名字大全 瀏覽:334
tplink默認無線密碼是多少 瀏覽:33
ipaddgm文件 瀏覽:99
lua語言編程用哪個平台 瀏覽:272
政采雲如何導出pdf投標文件 瀏覽:529
php獲取postjson數據 瀏覽:551
javatimetask 瀏覽:16
編程的話要什麼證件 瀏覽:94
錢脈通微信多開 瀏覽:878
中學生學編程哪個培訓機構好 瀏覽:852
榮耀路由TV設置文件共享錯誤 瀏覽:525

友情鏈接