【问题标题】:Read file, line by line, and store values into a array/string?逐行读取文件并将值存储到数组/字符串中?
【发布时间】:2015-05-23 06:16:38
【问题描述】:

我已经吸取了教训,所以我会很短,并且对主题。

我需要一个函数,在我的班级中,它可以逐行读取文件,并将它们存储到数组/字符串中,以便我可以使用它。

我有以下例子(请不要笑,我是初学者):

int CMYCLASS::LoadLines(std::string Filename)
{

    std::ifstream input(Filename, std::ios::binary | ios::in);
    input.seekg(0, ios::end);
    char* title[1024];
    input.read((char*)title, sizeof(int)); 

    // here what ?? -_-

    input.close();


    for (int i = 0; i < sizeof(title); i++)
    {
            printf(" %.2X ";, title[i]); 
    }

    printf("\");

    return 0;
}

【问题讨论】:

  • 使用std::stringstd:.vector。使用std::getline 阅读。或者,您可能会使用 istream 迭代器,并且曾经推荐它是表明属于 C++ 内部人员群体的“最佳”方式,但实际上这只是混淆。显式循环非常适合传达循环操作。
  • @Cheers 和 hth。 - Alf 谢谢,但我不能使用 while() 来使用 getline...我需要另一个解决方案
  • @Patrael:如果“不能使用while”是您老师的要求,那么您最好逐字逐句地说明这组要求。除此之外,听起来毫无意义。
  • 我想在控制台中使用这个功能,所以我可以调试它是否正确地读取我的行,如果我使用 WHILE() 控制台停止,我相信我的其他东西没有加载...使用继续;没解决...

标签: c++


【解决方案1】:

我不确定你到底在问什么。

但是 - 下面是一些逐行读取文件并将行存储在向量中的代码。该代码还打印行 - 既作为文本行,也作为每个字符的整数值。希望对您有所帮助。

int main()
{
    std::string Filename = "somefile.bin";
    std::ifstream input(Filename, std::ios::binary | ios::in);  // Open the file
    std::string line;                                           // Temp variable
    std::vector<std::string> lines;                             // Vector for holding all lines in the file
    while (std::getline(input, line))                           // Read lines as long as the file is
    {
       lines.push_back(line);                                   // Save the line in the vector
    }

    // Now the vector holds all lines from the file
    // and you can do what ever you want it

    // For instance we can print the lines
    // Both as a line and as the hexadecimal value of every character

    for(auto s : lines)                                         // For each line in vector
    {
        cout << s;                                              // Print it
        for(auto c : s)                                         // For each character in the line
        {
            cout << hex                                         // switch to hexadecimal
                 << std::setw(2)                                // print it in two 
                 << std::setfill('0')                           // leading zero
                 << (unsigned int)c                             // cast to get the integer value
                 << dec                                         // back to decimal
                 << " ";                                        // and a space
        }
        cout << endl;                                           // new line
    }
    return 0;
}

我不会因为你的原始代码而笑 - 没办法 - 我曾经也是一个初学者。但是您的代码是 c 风格的代码,并且包含很多错误。所以我的建议是:请改用 c++ 风格。例如:永远不要使用 C 风格的字符串(即 char 数组)。太容易出错了……

由于您是初学者(您自己的话:)让我解释一下您的代码:

char* title[1024];

这不是一个字符串。它是 1024 个指向字符的指针,也可以是 1024 个指向 c 样式字符串的指针。但是 - 您没有为保存字符串保留任何内存。

正确的方法是:

char title[1024][256];  // 1024 lines with a maximum of 256 chars per line

在此您必须确保输入文件的行数少于 1024 行且每行少于 256 个字符。

这样的代码非常糟糕。如果输入文件有1025行怎么办?

这就是 c++ 可以帮助您的地方。使用 std::string 您无需担心字符串的长度。 std::string 容器将根据您放入其中的大小进行调整。

std::vector 就像一个数组。但没有固定大小。所以你可以继续添加它,它会自动调整大小。

所以 c++ 提供了 std::string 和 std::vector 来帮助你处理输入文件的动态大小。使用它...

祝你好运。

【讨论】:

  • 非常感谢,这解决了我的问题!我不能投票,因为我还没有足够的声誉:(但是非常感谢!
猜你喜欢
  • 2021-07-13
  • 2017-09-29
  • 1970-01-01
  • 2015-12-31
  • 1970-01-01
  • 2016-07-27
  • 2012-08-18
  • 1970-01-01
  • 2014-05-03
相关资源
最近更新 更多