c語言 檔案讀寫(學習筆記)

2021-10-11 18:34:50 字數 527 閱讀 3804

file *fp = fopen("text.txt","r");

char ch,str[50];

/*向檔案輸入單個字元

ch = getchar();

while(ch != '#')

*//*從檔案中或如單個字元

while(!feof(fp))

*//*向檔案輸入字串

gets(str);

puts(str);

fputs(str,fp);  //學習筆記,若輸入的字元長度大於等於字元陣列容量,str不會被寫入檔案。

*//*從檔案中讀入指定長度字串

fgets(str,10,fp);

puts(str);

*//*用格式化的方式讀寫檔案(一般用於讀寫數字資料)

int a,b;

//fprintf(fp,"%d %d",2,3);

fscanf(fp,"%d%d",&a,&b);

printf("%d %d",a,b);

*/fclose(fp);

C語言學習筆記 檔案讀寫

如何開啟 建立 關閉文字檔案或二進位制檔案?我們通常使用fopen 函式來建立新檔案或開啟乙個已存在的檔案,這樣呼叫會初始化型別file的乙個物件,型別file包含了所有用來控制流的必要的資訊。file fopen const char filename,const char mode filena...

C語言檔案讀寫

include include define maxlen 1024 int main file outfile,infile outfile fopen 1.bmp wb infile fopen c 1.bmp rb unsigned char buf maxlen int rc while r...

C語言讀寫檔案

c語言庫函式包括檔案的開啟 關閉 讀 寫 定位等各種操作 要操作檔案,首先要宣告乙個檔案指標變數file fp 呼叫fopen開啟檔案 檔案指標名 fopen 檔名,使用檔案方式 ps 這裡檔案路徑必須是帶雙斜槓 其中,1.檔案使用方式 意 義 rt 唯讀開啟乙個文字檔案,只允許讀資料 wt 只寫開...