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

熱點內容
cudp文件傳輸 瀏覽:452
mac文件保存找不到桌面 瀏覽:614
數控編程軟體使用最多是哪個 瀏覽:324
root版本 瀏覽:61
html表格顏色代碼 瀏覽:805
java雙目運算符 瀏覽:159
qq古代唯美女生頭像 瀏覽:893
app資料庫伺服器配置 瀏覽:938
如何在電視上安裝米家app 瀏覽:991
二手物品哪個網站好 瀏覽:943
jdk轉換時找不到文件 瀏覽:345
在電腦上怎樣刪除文件夾 瀏覽:708
魅族手機黑屏找不到文件 瀏覽:729
如何找回qq幾年前的數據 瀏覽:867
不想學編程選什麼專業 瀏覽:97
華為g610一t11大聲音rom版本下載 瀏覽:645
背景音樂通常採用wav格式的音樂文件 瀏覽:218
java程序員提高班 瀏覽:67
為什麼要發明可編程式控制制器 瀏覽:846
電腦顯示佔用空間找不到文件 瀏覽:963

友情鏈接