我正在讀取文件orderedfile.txt
中的數據。有時,這種文件的形式的報頭:跳過C中文本文件的標題頭
BEGIN header
Real Lattice(A) Lattice parameters(A) Cell Angles
2.4675850 0.0000000 0.0000000 a = 2.467585 alpha = 90.000000
0.0000000 30.0000000 0.0000000 b = 30.000000 beta = 90.000000
0.0000000 0.0000000 30.0000000 c = 30.000000 gamma = 90.000000
1 ! nspins
25 300 300 ! fine FFT grid along <a,b,c>
END header: data is "<a b c> pot" in units of Hartrees
1 1 1 0.042580
1 1 2 0.049331
1 1 3 0.038605
1 1 4 0.049181
有時無標頭存在並且在第一行中的數據開始。我的數據讀取代碼如下所示。它在數據從第一行開始時起作用,但不在頭中出現。有沒有辦法解決這個問題?
int readinputfile() {
FILE *potential = fopen("orderedfile.txt", "r");
for (i=0; i<size; i++) {
fscanf(potential, "%lf %lf %*f %lf", &x[i], &y[i], &V[i]);
}
fclose(potential);
}
切換到讀取整行。這允許您檢測標題,然後讀取,直到數據開始。 – Yunnosch