逐行读取文件并将值存储到数组/字符串中?

3

我已经吸取了教训,所以我会简短明了地表达。

我需要在我的类中编写一个函数,能够逐行读取文件,并将其存储到数组/字符串中,以便我可以使用它。

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

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;
}

1
使用std::stringstd::vector。使用std::getline进行读取。或者您可以使用istream迭代器,曾经推荐它是“展示自己属于C++内部人士”的方式之一,但实际上这只是混淆视听。显式循环对于传达循环操作非常有用。 - Cheers and hth. - Alf
@Patrael:如果“不能使用while”是你老师的要求,那么最好全文逐字陈述一下需求。除此之外,这听起来毫无意义。 - Cheers and hth. - Alf
我想在控制台中使用此函数,以便进行调试,以查看它是否正确读取我的行,并且相信我,如果我使用WHILE(),控制台会停止运行,而我的其他内容也无法加载...使用continue;并没有解决这个问题... - Patratel
我认为您需要更详细地解释一下输入文件的外观以及您想要对其进行的操作。它是文本文件还是二进制文件?标题部分是什么?您当前的代码很奇怪。您只读取了 sizeof(int) 个字符到标题中(通常为4),但稍后您却打印了全部1024个字符。顺便说一句,在c++中,请使用std::string代替c风格的字符数组。此外,请使用cout而不是printf。 - Support Ukraine
@StillLearning - 谢谢,马尔。这是一个二进制文件,它将逐行包含所有异或的文本,但我无法阅读它...当然,我的代码很奇怪,因为我是个初学者 :| - Patratel
显示剩余2条评论
1个回答

4

我不确定你的问题是什么。

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

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来帮助您处理输入文件的动态大小。使用它...

祝您好运。


非常感谢,这解决了我的问题!我不能投票,因为我还没有足够的声望:( 但是非常感谢! - Patratel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接