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

熱點內容
ae氣功特效教程 瀏覽:727
紙飛機網站是什麼 瀏覽:742
文件系統錯誤65535 瀏覽:644
迅雷沒下載完的文件怎麼打開 瀏覽:878
delphijson例子 瀏覽:180
js放在jsp中不生效 瀏覽:674
微信爆粉推廣員 瀏覽:372
雷霆戰機升級合成 瀏覽:35
加密u盤文件解密 瀏覽:84
如何自己開發自己的一個網站 瀏覽:888
微信讀書離線文件轉化 瀏覽:125
無線感測網網路層 瀏覽:749
央視專區app 瀏覽:976
蘋果手機屏幕失真 瀏覽:58
php無版權企業網站管理系統 瀏覽:222
多個文件如何發163郵箱 瀏覽:207
騎士游戲安卓 瀏覽:449
dnf90版本劍宗巨劍 瀏覽:191
換地方手機網路很差怎麼辦 瀏覽:546
手機WiFikindle文件 瀏覽:226

友情鏈接