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

C++ 逐行读取文件,并将值存储到数组/字符串中?,c++,C++,我已经吸取了教训,所以我将简短地说,并将继续下去 在我的类中,我需要一个函数,它可以逐行读取文件,并将它们存储到数组/字符串中,以便我可以使用它 我举了下面的例子(请不要笑,我是个乞丐): int CMYCLASS::LoadLines(std::string文件名) { std::ifstream输入(文件名,std::ios::binary | ios::in); input.seekg(0,ios::end); 字符*标题[1024]; 读取((char*)标题,sizeof(int));

我已经吸取了教训,所以我将简短地说,并将继续下去

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

我举了下面的例子(请不要笑,我是个乞丐):

int CMYCLASS::LoadLines(std::string文件名)
{
std::ifstream输入(文件名,std::ios::binary | ios::in);
input.seekg(0,ios::end);
字符*标题[1024];
读取((char*)标题,sizeof(int));
//这是什么-_-
input.close();
对于(int i=0;i
我不太清楚你到底在问什么

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

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;
}
这不是字符串。它是1024个指向字符的指针,也可以是1024个指向c样式字符串的指针。但是,您没有为保存字符串保留任何内存

正确的方法是:

char title[1024][256];  // 1024 lines with a maximum of 256 chars per line
在这里,您必须确保输入文件的行数少于1024行,并且每行少于256个字符

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

<>这是C++帮助你的地方。使用std::string,您不需要担心字符串的长度。string容器将根据您输入的大小进行调整

向量就像一个数组。但是没有固定的尺寸。因此,您可以继续添加到它,它将自动调整大小

提供C++的STD::String和STD::vector,帮助您处理输入文件的动态大小。使用它


祝你好运。

使用
std::string
std:.vector
。使用
std::getline
进行读取。或者你可以使用一个ISTRAM迭代器,并且一次推荐它是“显示”一个属于C++内部人组的方法,但实际上它只是混淆。显式循环有助于传递循环操作。@干杯,谢谢。-Alf谢谢,但我不能使用while()来使用getline。。。我需要另一个solution@Patrael:如果您的老师要求“不能使用
,而
”,那么您最好完整、逐字地陈述这组要求。除此之外,它听起来毫无意义。我想在控制台中使用这个函数,这样我就可以调试它,看看它是否正确地读取了我的行,并告诉我是否在控制台停止,而我的其他东西没有加载时使用它。。。使用continue;没有解决它…非常感谢,这解决了我的问题!我不能投赞成票,因为我还没有足够的声望:(但非常感谢!
char* title[1024];
char title[1024][256];  // 1024 lines with a maximum of 256 chars per line