【问题标题】:Loading different data types from text file从文本文件加载不同的数据类型
【发布时间】:2015-05-27 17:25:50
【问题描述】:

我是 StackExchange 和 C++ 的新手,如果我没有很好地描述这个问题,我深表歉意。我需要一些家庭作业的帮助。 我正在尝试找到一种方法来加载此文件并存储其数据。该文件具有不同的数据类型,所以到目前为止我想出的是使用 struct array 来存储所有值,使用三重嵌套 for 循环将数据保存在正确的变量中。

这是 .dat 文件的样子,用“cmets”帮助描述正在发生的事情。

100A 2     // model number, 2 different versions of that model

0 0 0      // number of quarters, dimes, and nickels

5          //number of items in the vending machine

1A 1034  5 // Code combonation, ID Number, Quantity

1B 1000 10

1C 1100 10

1D 1123 20

1E 1222  5

0 0 0      // number of quarter, dimes, nickels in 2nd model

7          // number of items in the second version of that model

1A 2180 20

1B 1283 20

1C 3629  5

1D 3649  3

1E 4051 15

1F 4211  1

1G 5318  5

100B 3     // New model, with 3 different versions of itself.

2 10 5     //everything repeats like model 100A

7 

这是我想出的代码

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct VMdata
 {
    ifstream inFile;
    string model[1];
    int version[1];
    int q[5];
    int d[5];
    int n[5];
    int size[5];
    string id[30];
    int code[30];
    int num[30];
    char dummy;
};

int main()
{

    VMdata New;

    cout << fixed << setprecision(2) << showpoint;

    New.inFile.open("machines.dat");



    cout << "Model Data" 
           << endl   << endl;      



    int count1 = 0;
    int count2= 0;
    int count3 = 0;
    for (int i = 0; i < 2; i ++)
    {
        cout << "i :" << count1 << endl;
       New.inFile >> New.model[i] >> New.version[i]; // loads model number and number of versions i times
       count1 = count1 + 1;
       for (int j = 0; j < New.version[count1 -1]; j ++)
       {
           cout <<"j :" << count2 << endl;
       New.inFile >> New.q[count2] >> New.d[count2] >> New.n[count2]  >> New.size[count2]; // loads number of q, d, n, j times
       count2 = count2 + 1;
       for (int k = 0; k < New.size[count2 - 1]; k++)
        {
            cout << "k :" << count3 << endl;
           New.inFile >> New.id[count3] >> New.code[count3] >> New.num[count3]; // loads id number, code number, and total number k times
           count3 = count3 + 1;
        }

       }
    }
    New.inFile.close();

     count1 = 0;
    count2= 0;
    count3 = 0;
    cout << endl;

        for ( int i = 0; i < 2; i ++)
    {
       cout << New.model[i]  << setw(12) << New.version[i] << endl << endl;
        count1 = count1 + 1;
       for (int j = 0; j < New.version[count1 -1]; j ++)
       {
       cout << New.q[count2] << setw(12) << New.d[count2] << setw(12) << New.n[count2] << endl << setw(12) << New.size[count2] << endl;
      count2 = count2 + 1;
       for (int k = 0; k < New.size[count2 - 1]; k++)
        {
           cout << New.id[count3] << setw(12) << New.code[count3] << setw(12) << New.num[count3] << endl << endl;
           count3 = count3 + 1;
        }

       }
    }



      return 0;
}

这是我的测试输出。

100A           2

3          15           0 // should be 0, 0, 0

 5

1A        1034           5

1B        1000          10

1C        1100          10

1D        1123          20

1E        1222           5

0           0           0

  7

1A        2180          20

1B        1283          20

1C        3629           5

1D        3649           3

1E        4051          15

1F        4211           1

1G        5318           5

♥       ☻            3 // the heart and smile should be 100B lol

2          10           5

  7

1A        2180          10

1B        1283          10

1C        3629           5

1D        3649           3

1E        4051          15

1F        4211          10

1G        3026           5

5           6           3

  6

1A        6626           5

1B        6155           5

1C        5982          10

1D        5573           3

1E        5454          10

1F        5336          50

10          10          10

 5

1A        1034           5

1B        1000           5

1C        1100           5

1D        1123           5

1E        1210          12

Press any key to continue . . .

如您所见,第一台机器的 25 美分、硬币和镍币的数量是错误的,而第二台机器的型号名称是错误的。 如果有人有任何建议,将不胜感激。

【问题讨论】:

  • 对于这种输入,我会将整行读取为字符串,然后将其解析为单独的变量(结构)。
  • 嗨蒂姆,你能举个例子,把一行读成字符串并将它放在不同的变量中吗?我想过做这样的事情,但我不确定该怎么做,也不知道如何将字符串转换为 int。谢谢。
  • 抱歉,这里没有示例。使用 getline 将行读取为字符串,并使用 boost::split 将行拆分为字符串向量,扫描向量中的每个元素并将它们转换为正确的类型和目标。

标签: c++ file loops types text-files


【解决方案1】:

看起来您正在破坏数据。您的结构仅分配一个模型和一个版本,但 i 索引采用 0 和 1 的值。第二次读取这些值时,地址将覆盖您的硬币计数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 2013-09-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多