简体   繁体   English

从文件输入不同类型的数据,不使用向量

[英]Input data of different types from a file and NOT USING VECTORS

First off I am new to C++ so dont mark me down for not knowing something or being incorrect.首先,我是 C++ 的新手,所以不要因为我不知道某些事情或不正确而标记我。 I have a file that I am trying to read into my C++ program.我有一个文件,我试图将其读入我的 C++ 程序。 I am doing a practice for a project.我正在做一个项目的练习。 I have to create and object, and where each instance represents contines one line of the file with seven variables.我必须创建和对象,每个实例代表包含七个变量的文件的一行。 The file contains a database.该文件包含一个数据库。 The format of the text file is:文本文件的格式为:

// String String Character Int Int Char Int 
// String String Character Int Int Char Int
// etc. 

There is exactly 350 data members.正好有 350 个数据成员。 There is never more.再也没有了。 So I am using arrays.所以我正在使用数组。 I can not use vectors for this project as it is specified not to.我不能在这个项目中使用向量,因为它被指定不使用。

What is the best way to approach this with only using arrays?仅使用数组来解决这个问题的最佳方法是什么?

my first attempt was to create a 3 arrays:我的第一次尝试是创建一个 3 个数组:

        for (int l = 0; l < max_size; l++) {
            myfile >> myintarray[l];
        }
        for (int k = 0; k < max_size; k++) {
            myfile >> mychararray[k];
        }
       for (int i = 0; i < max_size; i++) {
            myfile >> myarray[i];
            // print artists and title; 
        }

       // Then I have to put it the object. Store as such....
        myloop = 0;
        a = 2;
        b = 5;
        while (myloop < 50) {
            myartwork[myloop].setRoom(mychararray[b]);
            myartwork[myloop].myart.setMeduim(mychararray[a]);
            a += 7;
            b += 7;
            myloop += 1;
        }
       // more code where I do the same thing with characters
       // same things with strings.....

When I tried that it printed off the contents.当我尝试它时,它打印了内容。 it was inccorect.这是不正确的。 and mumbo jumbo.和笨蛋。 So I think it is because I am when I try to store the all number array it cant take characters and prints out an error?所以我认为这是因为当我尝试存储所有数字数组时,它不能接受字符并打印出错误? I am not sure if that is correct.我不确定这是否正确。

So then I tried to use only one array that was string then convert it each string into a char or int type.然后我尝试只使用一个字符串数组,然后将每个字符串转换为 char 或 int 类型。 and that was a pain and I get an error.这很痛苦,我得到了一个错误。 and I am not sure how to handdle the character type.我不知道如何处理字符类型。

            myloop = 0;
            a = 3;
            b = 4;
            c = 6;
            while (myloop < 50) {
                //std::stoi (str_dec,&sz)
                std::string::size_type sz;

                // my object ( instance). data......Try to convert this. 
                myartwork[myloop].mymysize.setLength(std::stoi(myarray[a],  &sz));
                myartwork[myloop].mymysize.setWidth(std::stoi(myarray[b], &sz));
                myartwork[myloop].setPrice(std::stoi(myarray[c], &sz));
                a += 7;
                b += 7;
                c += 7;
                myloop += 1;
            }

Is there a better way to do this then these two methods?有没有比这两种方法更好的方法呢? Am I on the right track?我在正确的轨道上吗?

Using many arrays with the same capacity is usually a sign of a poor design.使用许多具有相同容量的阵列通常是设计不佳的标志。

Let's talk modeling here.让我们在这里谈谈建模。 Let's define a record as a container of fields.让我们将记录定义为字段的容器。 The record would correspond with one text line in your file.该记录将与您文件中的一个文本行相对应。 So, model it:所以,建模:

struct Record
{
    std::string s1;
    std::string s2;
    char        c1;
    int         i1;
    int         i2;
    char        c1;
    int         i3;
};

Now you can declare a table, or an array of records:现在您可以声明一个表或一记录:

#define MAXIMUM_RECORDS (350)
Record database[MAXIMUM_RECORDS];

Some more things to do:还有一些事情要做:

  1. Overload operator >> to read from input file.重载operator >>以从输入文件中读取。
  2. Implement constructors, copy constructors and assignment operator.实现构造函数、复制构造函数和赋值运算符。
  3. Overload comparison operators: == , > , etc.重载比较运算符: ==>等。
  4. Overload operator << to print the record.重载operator <<以打印记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM