使用以下代码,我正在读取.txt
文件,但无法正确输出。如果我仅在项目名称(文本文件中的第二个项目)之后仅使用逗号定界符,它要么不能正确地中断第一个输入(使用所有逗号分隔符),要么不能正确地中断。
代码在这里:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inventoryFile;
string itemType[100], itemName[100], matCost[100], manHours[100];
// float matCost[100], manHours[100];
string temp;
inventoryFile.open("items2.txt");
cout << endl << endl;
if(!inventoryFile)
{
cout << "\n\nCan't read from file.\n";
exit(1);
}
int numItems=0;
getline(inventoryFile, temp, ',');
while(!inventoryFile.eof())
{
itemType[numItems] = temp;
inventoryFile >> itemName[numItems];
inventoryFile.ignore();
inventoryFile >> matCost[numItems];
inventoryFile.ignore();
inventoryFile >> manHours[numItems];
inventoryFile.ignore();
numItems++;
getline(inventoryFile, temp, ',');
}
for(int j=0; j<numItems; j++)
{
cout << "Item Type:\t" << itemType[j] << endl;
cout << "ITem Name:\t" << itemName[j] << endl;
cout << "Mats Cost:\t" << matCost[j] << endl;
cout << "Man Hours:\t$" << manHours[j] << endl << endl;
}
return 0;
}
items.txt
文件是:
1, Xiphos, 7.46, 2
2, Dao, 3.45, 2.7
3, Jian, 2.31, 0.5
1, Rapier, 8.32, 2.3
2, Hook Sword, 2.11, 0.75
1, Panzerstecher, 2.23, 1.25
2, Kopis, 14.89, 2.3
3, Longsword, 5.43, 0.5
1, Tuck, 2.5, 15
1, Small Sword, 7.5, 2
3, Broadsword, 0.5, 0.25
items2.txt
文件是:
1 Xiphos, 7.46 2
2 Dao, 3.45 2.7
3 Jian, 2.31 0.5
1 Rapier, 8.32 2.3
2 Hook Sword, 2.11 0.75
1 Panzerstecher, 2.23 1.25
2 Kopis, 14.89 2.3
3 Longsword, 5.43 0.5
1 Tuck, 2.5 15
1 Small Sword, 7.5 2
3 Broadsword, 0.5 0.25
答案 0 :(得分:0)
这应该适用于items2.txt
中的格式。
string itemType[100], itemName[100];
float matCost[100], manHours[100];
//...
//getline(inventoryFile, temp, ',');
// get itemType and the whitespace after it
while(inventoryFile >> itemType[numItems] >> std::ws)
{
std::getline(inventoryFile, itemName[numItems], ',');
inventoryFile >> matCost[numItems] >> manHours[numItems];
// don't count this entry if the stream is in a failed state
if(inventoryFile.fail()) break;
inventoryFile.ignore();
++numItems;
}
答案 1 :(得分:0)
对于items.txt
,这应该有效:
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
struct Row {
int itemType;
string itemName;
float matCost;
float manHours;
};
istream& operator>>(istream& in, Row& row){
char comma;
char firstCharOfName;
in >> row.itemType >> comma >> firstCharOfName;
in.putback(firstCharOfName);
getline(in, row.itemName, ',');
return in >> row.matCost >> comma >> row.manHours;
}
int main() {
vector<Row> table;
{
ifstream inventoryFile("item.txt");
string line;
cout << "\n\n";
if (!inventoryFile) {
cerr << "\n\nCan't read from file.\n";
return EXIT_FAILURE;
}
while (getline(inventoryFile, line)) {
// make room for next row
istringstream iss(line);
table.resize(table.size() + 1U);
Row &newRow = table.back();
iss >> newRow;
if (!iss) {
// skip row on error
table.resize(table.size() - 1U);
}
}
}
for (int j = 0; j < table.size(); j++) {
cout << "Item Type:\t" << table[j].itemType << '\n'
<< "ITem Name:\t" << table[j].itemName << '\n'
<< "Mats Cost:\t$" << table[j].matCost << '\n'
<< "Man Hours:\t" << table[j].manHours << "\n\n";
}
return EXIT_SUCCESS;
}
PS:将$
移至了Mats Cost。