[英]Unable to read from external text file
所以我有一個結構類型的動態分配數組(我們稱之為structtype),我試圖從外部文本文件中讀取它。 structtype 看起來像這樣:
{
char itemName[15];
char status;
int itemAmt;
char tried;
char desc[130];
int rating;
}
文本文件看起來像這樣: SomeName: y | 3 | 你好你好| 是 | 9
以那種確切的格式。 我正在嘗試使用以下代碼從所述文本文件中讀取:
void games::LoadFrom(){ // Loads games from the file into array (AllItems)
ifstream file_in;
structtype temparray[15] = {};
for (int i = 0; i < 15; ++i) // Empties out AllItems
AllItems[i] = {};
file_in.open("items.txt");
int i = 0; // Counter
if (file_in){
while (!file_in.eof() && i < 15){ // Checks if its the end of the file, or if its the max amount I can store
file_in.get(temparray[i].itemName, 15, ':');
file_in.ignore(100, '|');
file_in >> temparray[i].status;
file_in.ignore(100, '|');
AllItems[i].status = temparray[i].status;
file_in >> temparray[i].itemAmt;
file_in.ignore(100, '|');
AllItems[i].playerAmt = temparray[i].itemAmt;
file_in.get(temparray[i].desc, 131, '|'); // All of this reads in a game from the file, and stores it in the AllItems array
file_in.ignore(100, '|');
file_in >> temparray[i].tried;
file_in.ignore(100, '|');
AllItems[i].tried = temparray[i].tried;
file_in >> temparray[i].rating;
file_in.ignore(100, '\n');
AllItems[i].rating = temparray[i].rating;
++i;
file_in.get(temparray[i].itemName, 15, ':');
file_in.ignore(100, ':');
}
cout << AllItems[0].itemAmt;
}
cout << AllItems[0].itemAmt;
file_in.close();
}
我的問題是它沒有讀取任何內容,而是將結構 object 中的所有內容都保留為零等效值。
重要的是為了理解要記住:AllItems 是一個動態分配的數組,我無法直接讀取它的內容,而是創建了一個名為 temparray 的單獨的靜態分配數組,它嚴格用於將信息傳輸到 AllItems
當循環運行時,它運行一次,然后退出,即使文本文件有大約 10 個項目。 你們能幫我弄清楚如何實際閱讀項目嗎? 謝謝!
咳咳咳。 我知道這可能很令人困惑,所以請隨時提出問題我會盡快回答。
這是您的代碼的外觀。 我忽略了AllItems
和AllGames
arrays,只專注於temparray
while (!file_in.eof() && i < 15){ // Checks if its the end of the file, or if its the max amount I can store
file_in.getline(temparray[i].itemName, 15, ':');
file_in >> temparray[i].status;
file_in.ignore(100, '|');
file_in >> temparray[i].itemAmt;
file_in.ignore(100, '|');
file_in.getline(temparray[i].desc, 130, '|'); // All of this reads in a game from the file, and stores it in the AllItems array
file_in >> temparray[i].tried;
file_in.ignore(100, '|');
file_in >> temparray[i].rating;
file_in.ignore(100, '\n');
++i;
}
與您的代碼的主要區別 - 我使用getline
not get
所以終止字符( ':'
或'|'
)被讀取並丟棄。 當需要丟棄尾隨分隔符時,我只在格式化提取之后(即在>>
之后)使用ignore
。
當您編寫此類代碼時,您必須詳細了解每次讀取 function 的作用,以及它如何影響您擁有的文件格式。 將一堆與您的文件格式大致相同的代碼放在一起是不夠的,它必須完全正確。
編輯
正如下面的評論所指出的,上面的代碼有一個問題,那就是它如何處理文件結尾。 它會導致在讀取之前而不是之后測試eof
的常見錯誤。 這是一個正確測試的版本。
while (i < 15 &&
file_in.getline(temparray[i].itemName, 15, ':') &&
file_in >> temparray[i].status &&
file_in.ignore(100, '|') &&
file_in >> temparray[i].itemAmt &&
file_in.ignore(100, '|') &&
file_in.getline(temparray[i].desc, 130, '|') &&
file_in >> temparray[i].tried &&
file_in.ignore(100, '|') &&
file_in >> temparray[i].rating &&
file_in.ignore(100, '\n'))
{
++i;
}
在這個版本中,只有當所有讀取都成功時才會執行 while 循環並且i
會遞增。
由於文件結束或格式錯誤,讀取可能會失敗。 如果您需要區分這兩種情況,則需要一些更復雜的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.