C 用fscanf()函式讀取TXT檔案內容

2021-07-24 13:34:51 字數 835 閱讀 7267

fscanf()函式的標頭檔案是,函式原型為int fscanf(file*stream, constchar*format, [argument...]); 其功能為根據資料格式(format)從輸入流(stream)中寫入資料(argument);與fgets的差別在於:fscanf遇到空格和換行時結束,注意空格時也結束,fgets遇到空格不結束。

ex:現在有這樣乙個資料檔案,想用fscanf函式讀取,每列作為乙個陣列:

1.000000e+000 2.000000e+000 -3.000000e+000

2.000000e+000 3.000000e+000 -2.000000e+000

3.000000e+000 4.000000e+000 -1.000000e+000

4.000000e+000 5.000000e+000 0.000000e+000

5.000000e+000 6.000000e+000 1.000000e+000

6.000000e+000 7.000000e+000 2.000000e+000

7.000000e+000 8.000000e+000 3.000000e+000

8.000000e+000 9.000000e+000 4.000000e+000

9.000000e+000 1.000000e+001 5.000000e+000

1.000000e+001 1.100000e+001 6.000000e+000

**如下:

# include int main(void)

fclose(fq);

return 0;

}

用C 讀取txt檔案的方法

1 使用filestream讀寫檔案 檔案頭 using system using system.collections.generic using system.text using system.io 讀檔案核心 byte bydata new byte 100 char chardata ne...

用C 讀取txt檔案的方法

1 使用filestream讀寫檔案 檔案頭 using system using system.collections.generic using system.text using system.io 讀檔案核心 byte bydata new byte 100 char chardata ne...

C 讀取txt檔案

1.逐行讀入 void readtxt string file ifstream infile infile.open file.data 將檔案流物件與檔案連線起來 assert infile.is open 若失敗,則輸出錯誤訊息,並終止程式執行 string s while getline i...