解析和读取文本文件到数组
C++: Parsing and Reading Text File Into an Array
我希望我能在这里找到一些帮助。我下周有个作业要交,包括从一个txt文件中读取一堆数据到一个数组中,然后把结果打印出来。数据格式如下:
"麦克白"、"莎士比亚","41.04","161","23"、"978-88-5985-004-5"
"圣诞颂歌","查尔斯·狄更斯","98.74","167","547","978-26-2885-780-7"。.
.
.
每行有6条信息,我需要存储这些信息以供以后使用。我应该写代码来计算文本的行数,以便创建一个大小正确的动态数组。我已经搞定了。我有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;
}
}
}
也许你只需要知道如何处理字符串中的每个字符?
下面是一些代码,它们遍历字符串中的每个字符来构建单词,然后分别打印它们。您会注意到string
与vector
(str[i]
, str.push_back(char)
, str.size()
等)具有相同的接口。
// You'll need to include <iostream> and <string>
std::string example = "This is an example string";
std::string word;
// Notice how you can loop through a string just like a vector<char>
for(size_t i = 0; i < example.size(); ++i) {
char c = example[i];
// When we see whitespace, print the current word and clear it
if(c == ' ' || c == 't' || c == 'n') {
// Don't print anything if we don't have a word
if(!word.empty()) {
std::cout << word << std::endl;
word.clear();
}
} else {
// Append the current character to the end of the string
word += c; // or word.push_back(c)
}
}
// In case the line doesn't end with whitespace
if(!word.empty()) {
std::cout << word << std::endl;
}
std::basic_string
(std::string
的别名)引用可能是有用的
可以使用一个向量数据结构来保存book类。向量记录;
(我强烈建议使用向量(或列表),因为它将避免文件的双重读取,因为您根本不需要知道行数)
解析具有固定数量字段的行,原则上很容易:
int counter = 0;
string myString;
while(!myFile.eof())
{
getline(myFile, myString);
counter++;
}
counter --;
//Clear the error state flag
myFile.clear()
//Return to the beginning of the file:
myFile.seekg(ios_base::beg);
const int fieldCount = 5;
string field[fieldCount ];
string buffer= "";
char c = ' ';
for( int i = 0; i < counter; ++i ) {
for( int j = 0; j < fieldCount; ++j ) {
myFile.ignore(); //Ignore the first '"'
//Read each character up to the second '"'
while( myFile.good() && (c = myfile.get()) != '"' ) {
buffer += c;
}
field[j] = buffer;
buffer = "";
if( j != fieldCount - 1 ) {
myFile.ignore(); //Ignore the first ','
}
}
//Use the fields here.
}
我没有测试这段代码,我知道缺少错误测试,但是它显示了一种方法。
- 如何将内容数组写入文本文件?
- C++将文本文件中的数据读取到结构数组中
- 将值从二维数组输出到文本文件
- C++数据文件、数组和计算赋值
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 如何从txt文件中读取多个不同长度的数组?
- 保存/加载大量短数组到二进制文件
- 从二进制文件中读取整数数组
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- C++编程从外部文本文件定义数组大小
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 如何将文本文件的特定行读取到 int 类型的数组中C++?
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 将系数存储在头文件的数组中("does not name a type"错误)
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 从文件读取,并写入函数C++中的数组
- 如何使用QJSONARRAY在QT5(C )中解析JSON文件(数组)
- 从文件/数组C++聚合数据