簡體   English   中英

將行從 txt 文件傳遞到數組時遇到問題

[英]Having trouble passing lines from txt file to array

我覺得我完全錯過了一些東西,但是當我測試我的數組是否被 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 調用 function。

這里有一點誤解:

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 ,你需要自己處理 memory。 這意味着使用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.       

它是安全的(嗯,更安全),有點可重復使用,並且封裝良好。

您需要使用正確的大小初始化 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();
}

看起來您正在嘗試使用變量作為數組長度來初始化myArray ,這在 C++ 中是不允許的。

由於您不能在此代碼中使用向量,因此您需要使用new運算符為數組分配空間。

所以首先,你需要將flag初始化為 0。然后,一旦你計算了文件中的行數,同樣創建myArray

string *myArray = new string[flag];

這條線的作用是,它在堆上為myArray分配了大小為flags的 memory 。

所以你的代碼應該是這樣的:

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

完成數組后,您可以使用delete[]運算符取消分配數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM