C++将数据文件中的行读入某些变量
C++ reading in lines from data file to certain variables
我必须从数据文件中读取专辑名称、歌曲和专辑年份的列表,然后按字母顺序对专辑名称和这些专辑中的歌曲名称进行排序。我可以读取整个文件,但我不能像通常使用数组那样修改数据文件。
这就是我到目前为止所拥有的。
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main() {
int n = 30;
int albums[n];
string content;
ifstream infile;
infile.open("BeatlesData.txt");
while (getline(infile,content)) {
cout <<content<< endl;
}
infile.close();
这是我试图从中读取的数据文件。每张专辑都用"="分隔,所以我在阅读时也必须考虑到这一点。
The Beatles White Album
Year: 1968
1. Back in the U.S.S.R.
2. Dear Prudence
3. Glass Onion
4. Ob-La-Di, Ob-La-Da
5. Wild Honey Pie
6. The Continuing Story of Bungalow Bill
7. While My Guitar Gently Weeps
8. Happiness Is a Warm Gun
9. Martha My Dear
10. I'm So Tired
11. Blackbird
12. Piggies
13. Rocky Raccoon
14. Don't Pass Me By
15. Why Don't We Do It in the Road?
16. I Will
17. Julia
18. Birthday
19. Yer Blues
20. Mother Nature's Son
21. Everybody's Got Something to Hide Except Me and My Monkey
22. Sexy Sadie
23. Helter Skelter
24. Long, Long, Long
25. Revolution 1
26. Honey Pie
27. Savoy Truffle
28. Cry Baby Cry
29. Revolution 9
30. Good Night
===============================
Abbey Road
Year: 1969
1. Come Together
2. Something
3. Maxwell's Silver Hammer
4. Oh! Darling
5. Octopus's Garden
6. I Want You (She's So Heavy)
7. Here Comes the Sun
8. Because
9. You Never Give Me Your Money
10. Sun King
11. Mean Mr. Mustard
12. Polythene Pam
13. She Came in Through the Bathroom Window
14. Golden Slumbers
15. Carry That Weight
16. The End
17. Her Majesty
===============================
Magical Mystery Tour
Year: 1967
1. Magical Mystery Tour
2. The Fool on the Hill
3. Flying
4. Blue Jay Way
5. Your Mother Should Know
6. I Am the Walrus
7. Hello Goodbye
8. Strawberry Fields Forever
9. Penny Lane
10. Baby You're a Rich Man
11. All You Need Is Love
===============================
Sgt. Pepper's Lonely Hearts Club Band
Year: 1967
1. Sgt. Pepper's Lonely Hearts Club Band
2. With a Little Help from My Friends
3. Lucy in the Sky With Diamonds
4. Getting Better
5. Fixing a Hole
6. She's Leaving Home
7. Being for the Benefit of Mr. Kite!
8. Within You Without You
9. When I'm Sixty-Four
10. Lovely Rita
11. Good Morning Good Morning
12. Sgt. Pepper's Lonely Hearts Club Band (Reprise)
13. A Day in the Life
===============================
Hey Jude
Year: 1970
1. Can't Buy Me Love
2. I Should Have Known Better
3. Paperback Writer
4. Rain
5. Lady Madonna
6. Revolution
7. Hey Jude
8. Old Brown Shoe
9. Don't Let Me Down
10. The Ballad of John and Yoko
-
你的问题是"问:我如何解析这个文件?"
-
A: 您有一个很好的开始——您想一次调用std::getline()一行,直到文件结束。
-
int albums[n];
是个坏主意。如果你有49张专辑,会发生什么?没有一个或者,最糟糕的是,有51张专辑?请改用std::vector。 -
"困难的部分"实际上是解析每一行。向我们展示一些工作,我们很乐意回答您遇到的具体问题的具体问题。
强烈建议:
- 考虑创建一个
Album
类 - 您的最终结果将是
vector<Album>
- 这个假设类可能有一个公共
parseLine(string line)
方法来读取每一行,直到相册完成("===") - 它还可能有专用的
parseTitle()
、parseYear()
和parseTrack()
方法,将输入文本读取到适当的类变量中 - 它可能也会有像
title
、year
和vector<Track>
这样的公共属性
你可以试试班级相册。制作一个矢量。专辑类内部有一个类轨道向量。相册类具有字段name和year。曲目有编号和名称字段。
前两行被读入专辑名称和专辑年份。接下来,将以下行读入字符串中,如果它不是以"="开头,则将其解析为音轨编号和名称。若你们找到了一个等号,那个么就开始读取专辑名称和年份,除非是文件末尾。
这不是标准的C++:
int n = 30;
int albums[n];
您必须更改为const int n
,或者最好使用std::vector<int>
。如果n
不是const
,则使用VLA(可变长度数组)扩展。
至于解析,算法应该如下:
- 第一行总是CD的名称(对吗?)
- 然后总会有一年
- N次歌曲名称,直到出现包含"====="的行,如果是,则跳到1。直到文件结束
所以在伪代码中:
struct record_type {
std::string name;
std::string year;
std::vector<std::string name> songs;
}
int main() {
// ...
std::vector<record_type> discs;
std::string content;
while(getline(infile,content)) {
record_type rec;
rec.name = content;
if (!getline(infile,content))
break;
rec.year = content;
while (getline(infile,content)) {
if ( content.find("=====")!=std::string::npos) break;
rec.songs.push_back(content);
}
discs.push_back(rec);
};
}
我并没有深入分析每一行,但这是您可以使用std::stringstream来完成的。例如解析年份:
std::stringstream str("Year: 1968 ");
int year;
std::string tmp;
str >> tmp >> year;
相关文章:
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 从模板创建通用打印函数,以打印基元数据类型变量的值
- 如何在变量中插入多种不同长度的数据类型?
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 面临在 if 语句之外打印变量数据的问题 完成使用 Qt 编程
- 有没有办法"QByteArray"变量数据直接放入变量"int"而无需强制转换?
- 如何在一行中从不同的变量(数据类型)创建一个字符数组?
- 从保存变量数据类型的数据结构中检索值,而不指定返回类型
- 从字符串中读取变量数据
- 返回对成员变量数据的引用的开销
- 如何将C 变量数据放入System()函数中
- C++生成器 XE 2 -- 如何在 TEdit 控件中显示变量数据
- 定义C++变量:数据类型(值)
- 静态变量/数据和对象
- 将非成员变量数据传递给构造函数时,如何保存它们并在其他成员函数中使用?C++
- 将变量数据发送到其他类时出现问题
- 如何从变量数据创建std::initializer_list
- 将c++的变量数据类型转换为c#
- 如何在C++中存储变量数据