我希望能在這裏找到一些幫助。我下週要完成一項任務,包括從txt文件中讀取一堆數據到數組中,然後打印出結果。的數據是在以下格式:C++:解析和讀取文本文件到數組
「麥克白」, 「莎士比亞」, 「41.04」, 「161」, 「23」, 「978-88-5985-004-5」
「A聖誕頌歌「,」查爾斯狄更斯「,」98.74「,」167「,」547「,」978-26-2885-780-7「。 。
。
。
每一行都有六條信息,我需要存儲以供以後使用。我應該編寫代碼來計算我們擁有的文本行數,以便創建正確大小的動態數組。我已經覆蓋了。我有39行條目。然後我應該創建一個讀取txt文件的函數,並將所有數據保存到我創建的數組中的相應對象。
我不知道使用什麼方法,並且我一直在四處尋找教程和解釋幾天。我對文件和解析的經驗非常有限,所以如果我有點經驗不足,請原諒。這裏是我到目前爲止的代碼:
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
class Author
{
public:
private:
string fname, lname;
};
class Book
{
friend ofstream& operator<<(ofstream&, Book);
public:
Book();
private:
string bookName;
Author author;
double price;
int qtyOnHand;
int qtySold;
double revenue;
string ISBN;
};
Book :: Book()
{
}
int getLineNumber(ifstream &);
void parseData(ifstream &, Book []);
//void sortBookList(Book[], int, int);
int main()
{
int numberOfBooks;
//open the file from which to read the data
ifstream myFile;
myFile.open("Book List.txt");
//function to find out how many objects to create
numberOfBooks = getLineNumber(myFile);
//create an array with that many objects
Book *bptr;
bptr = new Book[numberOfBooks];
//function to read information from file into array of objects
parseData(myFile, bptr);
//loop to call sorting function and output function based on 4 different criteria
//close the file explicitly
return 0;
}
int getLineNumber(ifstream &myFile)
{
int counter = 0;
string myString;
while(!myFile.eof())
{
getline(myFile, myString);
counter++;
}
myFile.close();
counter --;
return counter;
}
void parseData(ifstream &myFile, Book bookPtr[])
{
}
所以,總結一下我的問題,我不明白如何從文本文件中的數據解析到我的數組。 非常感謝您提前幫助任何人!乾杯。
編輯:我試過與代碼搞混了,我想我在正確的方向邁出了一步,但我仍然有點迷路。這是我對parseData函數的看法。
void parseData(ifstream &myFile, Book bookPtr[])
{
string dummyLine;
string word, line;
myFile.open("Book List.txt");
getline(myFile, dummyLine);
string data[6];
while(!myFile.eof())
{
getline(myFile, line, '\n');
for (size_t i = 0; i < line.size(); ++i)
{
char c = line[i];
if(c == ',' || c == '\n')
{
if(!word.empty())
{
data[i] = word;
word.clear();
}
}
else
{
word += c;
}
}
if(!word.empty())
{
//cout << word << endl;
}
}
}
你在尋找什麼樣的功能? 'getline()'很有用,但你已經在使用它了。你似乎也已經知道如何使用字符串。你還需要什麼? – 2013-04-22 22:44:36
這個新代碼不起作用? – 2013-04-23 19:10:38