【问题标题】:Having trouble passing lines from txt file to array将行从 txt 文件传递​​到数组时遇到问题
【发布时间】:2021-08-25 09:16:58
【问题描述】:

我觉得我完全错过了一些东西,但是当我测试我的数组是否被 txt 文件中的值填充时,我的编译器完全没有显示任何内容。

void orderID(){
  ifstream result;

  int flag;
  int loop = 0;
  string temp;

  string line;
  string myArray[flag];

  result.open("resultat.txt");
  while(getline(result, line)){
    flag++; //number of lines in file
  }
  result.close();

  result.open("resultat.txt");
  while(getline(result, line)){
    myArray[loop] = line;
    loop++;
    cout<< myArray[1];
  }
  result.close();
}

可能应该发布 txt 文件进行即时测试。它应该适用于任何文件。

21 para 21 first
23 dyta 23 second
11 katert 11 fourth
12 pest 12 fifth
13 fundit 13 last
14 jojo 14 nono

有人知道我在这里缺少什么吗?顺便说一句,请不要建议向量,因为我不能在这种情况下使用它们。是的,我正在从 int main 调用该函数。

【问题讨论】:

  • 你初始化flag = 0;了吗?
  • 出现同样的问题。编辑:在我以前测试 cout
  • 如果你在知道它应该有多大之前就声明了它,你如何期望它的大小是正确的? C++ 也没有变长数组,请改用std::vector
  • 我确实说过我不能使用向量。我是在学校学习这门语言的初学者,你不必问我更多问题,我自己已经有很多问题了。
  • 使用 cout

标签: c++ arrays string fstream


【解决方案1】:

这里有点误会:

int flag; // uninitialised, this value could be anything
...
string myArray[flag]; // 1. you haven't set flag, how can we know how big this is
                      // 2. VLA are non-standard c++, you can't do this so easily

既然你说你不能使用std::vector,这有点可惜,你需要自己处理内存。这意味着使用new[]delete[]。像这样的:

int flag = 0
// Use your loop to find out the size of the vector
string* myArray = new string[flag]
// use myArray the same way (aka, get things with myArray[myIndex])
// make sure you check for out of bounds operations
delete[] myArray // This is super important, when you use new, you must use delete. 

这是基本的方法,但要真正让你惊叹的是,你的老师,只需写一个最小的向量!为此我们需要什么?

  1. 我们构建的东西只有可能的正确尺寸。
  2. 一种访问元素的方法。
  3. 正确销毁。

这样就足够了:

class MyVector {
  public:
    MyVector(unsigned size) { // Constructor for a specific size
        array_ = new string[size];
    }

    string& operator[](unsigned index) { // A way to access the elements
        return array_[index];
    }

    ~MyVector() { // A way to destroy it
        delete[] array_;
    }

    // You should really delete copy constructor and others (rule of 5) but that
    // is a little advanced for this. 

  private:
    string* array_;

};

然后你可以在你的代码中很好地使用它:

int flag = 0
// ...
MyVector myArray(flag);
// ...
    myArray[someIndex] = someThing;
//...
//... No need to delete, that is handled by the class.       

它是安全的(嗯,更安全),有点可重复使用,并且封装良好。

【讨论】:

    【解决方案2】:

    您需要使用正确的大小初始化 myArray,这意味着在您计算标志之后而不是在它未定义时

    void orderID(){
      ifstream result;
    
      int flag = 0;
      int loop = 0;
      string temp;
    
      string line;
    
      result.open("resultat.txt");
      while(getline(result, line)){
        flag++; //number of lines in file
      }
      result.close();
    
      string myArray[flag];
    
      result.open("resultat.txt");
      while(getline(result, line)){
        myArray[loop] = line;
        loop++;
        cout<< myArray[1];
      }
      result.close();
    }
    

    【讨论】:

    • 你说的是真的。稍后我不得不介绍这个数组。非常感谢您的解决方案
    • 这不符合 C++ 标准 ([stackoverflow.com/questions/17318007/…VLA))。对于动态数组,当 std::vector 由于某种原因不能使用时,应该使用 new[]/delete[]。它被多个编译器支持,所以如果它适合你,那很好。请注意,较大的数组可能会导致堆栈溢出。
    • 感谢您的意见,但我确实不明白您提到的一半 xd。我相信我会在不久的将来学习它们。
    【解决方案3】:

    您似乎正在尝试使用一个变量作为数组长度来初始化 myArray,这在 C++ 中是不允许的。

    由于您不能在此代码中使用向量,因此您需要使用 new 运算符为数组分配空间。

    首先,您需要将flag 初始化为0。然后,在计算完文件中的行数后,同样创建myArray

    string *myArray = new string[flag];
    

    这一行的作用是在堆上为myArray分配内存,大小为flags

    所以你的代码应该是这样的:

    flag = 0;
    while(getline(result, line)){
        flag++;
    }
    string *myArray = new string[flag];
    //...
    delete[] myArray;
    

    完成数组后,您可以使用 delete[] 运算符取消分配数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-22
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多