如何输入某个文件,同时将某些部分分配到不同的变量中
how to input a certain file, while allocating certain parts into different variables
我有一个文件,里面有猫王作品的唱片。我需要将它输入到我的程序中,同时将歌曲名称、发行日期、专辑名称和分秒分隔到不同的变量中。我没有太多的东西,我确信它充满了错误(还没有编译),但我真的很感激在这方面的帮助。事先非常感谢。这是我得到的。
编辑:我只需要输入帮助,而不是程序的其余部分
输入文件:
Elvis Presley
March 1956
1. Blue Suede Shoes 1:58
2. I'm Counting on You 2:24
3. I Got A Woman 2:23
4. One-Sided Love Affair 2:09
5. I Love You Because 2:42
6. Just Because 2:32
7. Tutti Frutti 1:59
8. Tryin' to Get to You 2:33
9. I'm Gonna Sit Right Down and Cry 2:05
10. I'll Never Let You Go 2:25
11. Blue Moon 2:43
12. Money Honey 2:36
=============================================
Blue Hawaii
November 1961
1. Blue Hawaii 2:39
2. Almost Always True 2:22
3. Aloha-Oe 1:54
4. No More 2:25
5. Can't Help Falling In Love 3:04
6. Rock-a Hula Baby 2:02
7. Moonlight Swim 2:22
8. Ku-U-I-Po (Hawaiian Sweetheart) 2:23
9. Ito Eats 1:25
10. Slicin' Sand 1:38
11. Hawaiian Sunset 2:37
12. Beach Boy Blues 2:07
13. Island of Love 2:42
14. Hawaiian Wedding Song 2:51
=============================================
Elvis' Christmas Album
October 1957
1. Santa Claus Is Back in Town 2:22
2. White Christmas 2:23
3. Here Comes Santa Claus 1:54
4. I'll Be Home for Christmas 1:53
5. Blue Christmas 2:07
6. Santa Bring My Baby Back (to Me) 1:54
5. O Little Town of Bethlehem 2:35
6. Silent Night 2:23
7. Peace in the Valley 3:22
=============================================
G.I. Blues (album)
October 1960
1. Tonight Is So Right for Love 2:14
2. What's She Really Like 2:17
3. Frankfort Special 2:58
4. Wooden Heart 2:03
5. G.I. Blues 2:36
6. Pocketful of Rainbows 2:35
7. Shoppin' Around 2:24
8. Big Boots 1:31
9. Didja' Ever 2:36
10. Blue Suede Shoes 2:07
11. Doin' the Best I Can 3:10
=============================================
Follow That Dream
April 1962
1. Follow That Dream 1:39
2. Angel 2:39
3. What A Wonderful Life 2:27
4. I'm Not The Marrying Kind 1:51
到目前为止,我对这个项目的了解。
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
char songName[256];
String AlbumName[5];
String ReleaseDate[5];
String SongName[5][14];
int hours[5];
int minutes[5][14];
int seconds[5][14];
for(int i = 0; i < 5; i++){
getline(cin,AlbumName[i]);
getline(cin,RelaseDate[i]);
for(int r = 0; r < 14; r++){
cin.get(SongName[i][r], 41);
cin.get(minutes[i][r], 42);
cin.get(seconds[i][r], 44);
}
}
}
在追求第一个答案后,我遇到了一堆错误,我可以看到它是如何工作的,但同样不是。
#include <iostream>
#include <iomanip>
//#include <fstream>
using namespace std;
struct Album
{
string albumName;
string releaseDate;
string songName;
int minutes;
int seconds;
//..... other members
};
int main()
{
struct Album albums[5][14];
// do other things
for (int i=0; i< 5;i++){
/// read in values like albums[i].albumName = value ...
cin.get(albums[i][0].albumName, 22);
cin.get(albums[i][0].releaseDate, 22);
for(int r=0; r< 14;r++){
char a;
cin >> a;
if(a == '='){
r == 14;
}
get(albums[i][r].songName, 39);
cin >> albums[i][r].minutes >> albums[i][r].seconds;
// cin >> albums[i][r].songName >> albums[i][r].minutes >> albums[i][r].seconds;
}
}
for(int i = 0; i<5;i++){
cout<<albums[i][0].albumName<<endl<<albums[i][0].releaseDate<<endl;
for(int r = 0; r<14;r++){
cout <<albums[i][r].albumName <<" "<< albums[i][r].minutes <<" "<< albums[i][r].seconds;
}
}
return 0;
}
您应该使用结构(可能命名为"专辑")来保存特定专辑的所有相关数据,如专辑名称、发行日期等。然后创建一个结构数组,从文件中读取数据值并存储在结构中。
struct Album
{
string albumName;
string releaseDate;
//..... other members
};
int main()
{
struct Album albums[5];
// do other things
for (int i=0; i< 5;i++){
/// read in values like albums[i].albumName = value ...
}
return 0;
}
相关文章:
- 将数组的地址分配给变量并删除
- vector.resize()中的分配错误
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Win32编译器选项和内存分配
- 函数中堆分配的效果与缺少堆分配的情况
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 获取字符串的长度并将其分配给数组
- 将地址分配给本地指针后,公共对象的变量将消失
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 我在二维向量中是否正确分配了内存
- 正在尝试重载二进制搜索树分配运算符
- GlobalAlloc而不是其他分配方法
- 自定义先决条件对移动分配运算符有效吗
- 我可以重新分配/覆盖std::字符串吗
- 在c++中使用动态分配的问题
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 为什么我可以使用比分配的内存更多的内存
- C++删除堆分配数据的二进制数组-它会全部删除吗?关于删除,我应该知道些什么