【问题标题】:C++ Read getline from txt file and store into string arrayC ++从txt文件中读取getline并存储到字符串数组中
【发布时间】:2021-10-25 07:52:35
【问题描述】:

我正在使用 (getline(MyFile, line)) 从文本文件中读取文本文件,并希望将其存储在每个元素的字符串数组中。 我的代码无法正常工作。 基本上,我需要从文件中读取,将每一行保存到一个字符串数组中,然后在每个元素处遍历每个字符串。 for 循环也没有增加到 4,我不知道为什么。请帮忙。

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

int array[16], i;
fstream myFile;
string filename, line, str[256];

int main() {

    // 1. The user will enter the full name of the file
    cout << "Enter the file name to open: ";
    cin >> filename;
    getchar();  // clear the input buffer before reading the filename

    // 2. Open the file for reading (input)
    myFile.open(filename.c_str(), fstream::in);

    // only continue if the file has opened successfully
    if (myFile.is_open() == false) {
        cout << "ERROR: not able to open " << filename << endl;
    }
    else {
        // 3. Access the file - read all the lines from the file
        for (i = 0; i < 5; i++) {
            while (getline(myFile, line)) {
                str[i] = line;
                cout << "String [" << i << "] = " << str[i] << endl;
            }
        }
    }
}

【问题讨论】:

  • 问问自己:当 i == 0 时读取了多少行?在调试器下运行程序,在str[i] = line 上设置断点,看看会发生什么。

标签: c++ arrays for-loop iteration getline


【解决方案1】:

我对您的代码进行了 2 次更改,以读取和打印文本文件的所有行(

1.注释掉for声明

2.在while循环中插入i++

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

int array[16], i;
fstream myFile;
string filename, line, str[256];

int main() {

    // 1. The user will enter the full name of the file
    cout << "Enter the file name to open: ";
    cin >> filename;
    getchar();  // clear the input buffer before reading the filename

    // 2. Open the file for reading (input)
    myFile.open(filename.c_str(), fstream::in);

    // only continue if the file has opened successfully
    if (myFile.is_open() == false) {
        cout << "ERROR: not able to open " << filename << endl;
    }
    else {
        // 3. Access the file - read all the lines from the file
        //for (i = 0; i < 5; i++) 
        {
            while (getline(myFile, line)) {
                str[i] = line;
                cout << "String [" << i << "] = " << str[i] << endl;
                i++;
            }
        }
    }
}

【讨论】:

    【解决方案2】:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    // get used to not typing, using namespace
    
    int main() 
    {
        std::fstream myFile;
        std::string filename;
        std::string line;
        std::vector<std::string> lines; // use vector instead of arrays
    
        // error handling in C++, is exceptions
        myFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    
        // 1. The user will enter the full name of the file
        std::cout << "Enter the file name to open: ";
        std::cin >> filename;
        //getchar();  // clear the input buffer before reading the filename
    
        // 2. Open the file for reading (input)
        try
        {
            myFile.open(filename.c_str(), std::fstream::in);
        
            // 3. Access the file - read all the lines from the file
            int count = 0;
            while (getline(myFile, line))
            {
                lines.push_back(line);
                std::cout << "String [" << count++ << "] = " << line << std::endl;
            }
        }
        catch (const std::exception& e)
        {
            std::cout << "ERROR: not able to open " << filename << " error = " << e.what() << std::endl;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-25
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      • 2018-11-01
      相关资源
      最近更新 更多