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

熱點內容
繞過改密碼登錄密碼登錄密碼登錄密碼登錄 瀏覽:450
風暴英雄當前版本最強 瀏覽:104
餘姚數控編程培訓哪裡專業 瀏覽:419
qq空間66版本下載 瀏覽:908
有一款看美劇的app是什麼 瀏覽:397
前端後端json資料庫 瀏覽:267
vi文件格式linux 瀏覽:963
php如何引用js文件 瀏覽:531
word轉成pdf怎麼設置漸變色背景 瀏覽:655
數控車床車刀如何編程 瀏覽:577
為什麼復制到u盤沒有文件 瀏覽:792
三星怎麼設置文件的打開方式 瀏覽:332
微信怎麼發大容量的文件夾 瀏覽:130
hl線切割編程怎麼旋轉圖形 瀏覽:234
qq頭像露全身 瀏覽:811
力量運動員影響哪些體檢數據 瀏覽:176
金山哪裡有加工中心編程培訓學校 瀏覽:113
壓縮文件正在壓縮怎麼取消 瀏覽:462
excel中添加文件路徑名 瀏覽:16
手機收銀用哪個app 瀏覽:229

友情鏈接