【发布时间】:2016-03-25 17:19:35
【问题描述】:
我有一个包含超过 5000 行数据的文本文件(乐透的抽奖结果)。每行的格式为:数字。日月年 number1,number2,number3,number4,number5,number6
五个示例行:
- 27.01.1957 8,12,31,39,43,45
- 03.02.1957 5,10,11,22,25,27
- 10.02.1957 18,19,20,26,45,49
- 17.02.1957 2,11,14,37,40,45
- 24.02.1957 8,10,15,35,39,49
我也有:
struct Lotto
{
short number_drawing;
char day;
char month;
short year;
char tab[6];
};
我必须将此文本文件中的数据作为 struct Lotto 写入二进制文件。
我的想法已经用完了。 几天以来我一直在尝试,但我的程序仍然无法正常工作:(
我尝试加载一行:)
int main()
{
ifstream text("lotto.txt", ios::in);
ofstream bin("lottoBin.txt", ios::binary | ios::out);
Lotto zm;
short number_drawing;
char day;
char month;
short year;
char tab[6];
char ch;
int records = 0;
while (!text.eof())
{
text >> zm.number_drawing >> ch >> zm.day >> ch >> zm.month >>
ch >> zm.year >> zm.tab[0] >> ch >> zm.tab[1] >> ch >> zm.tab[2] >>
ch >> zm.tab[3] >> ch >> zm.tab[4] >> ch >> zm.tab[5];
records++;
}
cout << "All records: " << records << endl;
【问题讨论】:
-
向我们展示您的程序
-
"binary file as struct Lotto" Lotto 是一个结构体。谁定义的?你还是你的老师?
-
我的老师定义的。
标签: c++ text-files fstream binaryfiles objective-c++