我是一個初學C++用戶,我曾嘗試與我的同學們合作,但我們一直未能找到這個問題的答案。我們的教師爲我們提供了一個鏈接器,它爲我們運行主要功能,並提供一個簡單的文本文件供我們讀取,目前第二個const char *在標題中是不重要的,因爲現在我需要的是從文件const char * saifFile中讀取數據並將其顯示在屏幕上。當我運行我的程序時,我發現它會提前停止閱讀。而且我知道你可能無法提供幫助,因爲你無法訪問鏈接器,但任何幫助都將不勝感激。我想知道爲什麼我的閱讀文件提前停止
這裏是我的所有代碼:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int DESC_SIZE = 37;
struct Item
{
int itemId;
char description[DESC_SIZE];
double cost, price;
};
int processFile(const char* saifFile, const char* raofFile)
{
fstream outFile, inFile;
Item Inventory;
inFile.open(saifFile, ios::in);
while (inFile)
{
inFile >> Inventory.itemId >> Inventory.cost >> Inventory.price;
inFile.getline(Inventory.description, DESC_SIZE);
cout << " " << Inventory.itemId << " " << setw(5) << Inventory.cost << " " << setw(5) << Inventory.price <<" " << Inventory.description << endl;
}
return 0;
}
你可以發佈你正在閱讀的文件的內容嗎? –
更喜歡使用'Inventory.description = std :: getline(infile)',所以你不必擔心緩衝區大小。 –
是'DESC_SIZE'的任意大小? – 109