我知道這是一個愚蠢的問題,但我將如何從多行文本文件加載數據?C,閱讀多行文本文件
while (!feof(in)) {
fscanf(in,"%s %s %s \n",string1,string2,string3);
}
^^這是我如何從一行加載數據,它工作正常。我只是不知道如何從第二行和第三行加載相同的數據。
同樣,我意識到這可能是一個愚蠢的問題。
編輯:問題沒有解決。我不知道如何從不在第一行的文件讀取文本。我將如何做到這一點?對不起,這個愚蠢的問題。
我知道這是一個愚蠢的問題,但我將如何從多行文本文件加載數據?C,閱讀多行文本文件
while (!feof(in)) {
fscanf(in,"%s %s %s \n",string1,string2,string3);
}
^^這是我如何從一行加載數據,它工作正常。我只是不知道如何從第二行和第三行加載相同的數據。
同樣,我意識到這可能是一個愚蠢的問題。
編輯:問題沒有解決。我不知道如何從不在第一行的文件讀取文本。我將如何做到這一點?對不起,這個愚蠢的問題。
試着這麼做:
/編輯/
char line[512]; // or however large you think these lines will be
in = fopen ("multilinefile.txt", "rt"); /* open the file for reading */
/* "rt" means open the file for reading text */
int cur_line = 0;
while(fgets(line, 512, in) != NULL) {
if (cur_line == 2) { // 3rd line
/* get a line, up to 512 chars from in. done if NULL */
sscanf (line, "%s %s %s \n",string1,string2,string3);
// now you should store or manipulate those strings
break;
}
cur_line++;
}
fclose(in); /* close the file */
,或者甚至...
char line[512];
in = fopen ("multilinefile.txt", "rt"); /* open the file for reading */
fgets(line, 512, in); // throw out line one
fgets(line, 512, in); // on line 2
sscanf (line, "%s %s %s \n",string1,string2,string3); // line 2 is loaded into 'line'
// do stuff with line 2
fgets(line, 512, in); // on line 3
sscanf (line, "%s %s %s \n",string1,string2,string3); // line 3 is loaded into 'line'
// do stuff with line 3
fclose(in); // close file
等等,所以如果我需要得到第二條線,我該怎麼做?謝謝你的幫助。 – 2011-04-29 04:57:29
看看編輯是否有效。 fgets將逐行獲取文件。 編輯基本上只是獲得第三行。 – jiman 2011-04-29 21:22:50
非常感謝,你的榜樣幫助我。 – 2011-04-29 22:13:38
將\n
設置爲scanf格式的字符串對空間沒有不同的影響。您應該使用fgets獲取該行,然後使用字符串本身的sscanf。
這也允許更容易的錯誤恢復。如果只是匹配換行符,則可以在字符串的末尾使用"%*[ \t]%*1[\n]"
而不是" \n"
。在這種情況下,您應該使用%*[ \t]
來代替所有空間,並檢查fscanf的返回值。在輸入中直接使用fscanf是非常困難的(如果一行上有四個單詞會發生什麼?如果只有兩個單詞會發生什麼?),我會推薦fgets/sscanf解決方案。
另外,正如Delan Azabani所說......從這個片段中不清楚你是否已經這樣做了,但是你必須定義空間[例如,在一個大型數組或一些帶有malloc的動態結構中]來存儲整個數據集,或者在循環中執行所有處理。
您還應該指定格式說明符中每個字符串有多少可用空間。在scanf中本身就是一個缺陷,可能是一個安全漏洞。
我意識到scanf可能是一個漏洞,但這不是我的問題。我知道字符串對於文件類型是正確的大小,我需要知道從文件的第二行和第三行以及第一行讀取數據的方式。我不知道該怎麼做。 – 2011-04-29 21:13:58
每次撥打fscanf
時,它會讀取更多值。你現在面臨的問題是你將每行重新讀入相同的變量,所以最後,這三個變量有最後一行的值。嘗試創建一個可以容納所有需要的值的數組或其他結構。
首先,你不使用feof()
像......它顯示了一個可能的帕斯卡背景,無論是在你的過去還是在你老師的過去。
對於閱讀線條,您最好使用POSIX 2008(Linux)getline()
或標準C fgets()
。無論哪種方式,你嘗試閱讀與功能的線,並停止在表示EOF:
while (fgets(buffer, sizeof(buffer), fp) != 0)
{
...use the line of data in buffer...
}
char *bufptr = 0;
size_t buflen = 0;
while (getline(&bufptr, &buflen, fp) != -1)
{
...use the line of data in bufptr...
}
free(bufptr);
讀取多個行,你需要決定是否需要對以前的線條。如果不是,一個單一的字符串(字符數組)將會做。如果你需要前面的行,那麼你需要讀入一個數組,可能是一個動態分配的指針數組。
我有一個令人困惑的方法,也不會有混淆片斷一種更簡單的解決方案(沒有冒犯到規定以上)這裏是:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;//read the line
ifstream myfile ("MainMenu.txt"); // make sure to put this inside the project folder with all your .h and .cpp files
if (myfile.is_open())
{
while (myfile.good())
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
快樂編碼
我仍然不幸的是,困惑。具體而言,例如,如果我想閱讀第三行,我該怎麼辦? – 2011-04-29 05:06:33