将文本文件读入字符串数组

Reading text file into string array

我正在尝试将一个文件对象的内容读入一个字符串数组,但是当我打印该数组的内容时,无论我尝试什么,都没有显示任何内容。具体来说,我想通过将文本文件的最后十行传递到数组并在数组上使用 for 循环来打印它们。

void FileReader::displayLast10records(){

    ifstream ifile(filename);
    string myArray[26];

    cout << "\n" << filename << ":  LAST 10 records in file \n\n";

    for (int i = 0; i < numrecords; i++)
        getline(ifile, myArray[i]);

    ifile.close();

    if (numrecords < 10)
    {
        for (int i = 0; i < numrecords; i++)
            cout << setw(2) << (i + 1) << ".\t" << myArray[i] << endl;
    }
    else if (numrecords > 10)
    {
        for (int i = (numrecords - 10); i < numrecords; i++)
        {
            cout << setw(2) << (i + 1) << ".\t" << myArray[i] << endl;
        }
    }
}

文件只是大块文本,包括空格。该文件如下所示:

A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely. The earliest programming languages predate the invention of the computer, and were used to direct the behavior of machines such as Jacquard looms and player pianos. Thousands of different programming languages have been created, mainly in the computer field, with many being created every year. Most programming languages describe computation in an imperative style, i.e., as a sequence of commands, although some languages, such as those that support functional programming or logic programming, use alternative forms of description.

我想将每一行读入它自己的字符串数组元素。

我确实有另一个函数成功地使用 getline() 一次显示文本文件的每一行 10 行。

void FileReader::displayAllRecords(){

ifstream ifile(filename);
int displayed_lines = 0;
string arec;

cout <<"\n" << filename << ": ALL records in the file with line numbers, 10 at a time \n\n";

while (getline(ifile, arec))
{
    if(displayed_lines % 10 == 0 && displayed_lines >= 1)
        system("pause");

    cout << setw(2) << (displayed_lines + 1) << ".\t" << arec << endl;
    displayed_lines++;
}


ifile.close();
}

您的文件可能没有像您的程序所期望的那样包含正确的行结束标记,但您的代码假设该文件刚刚 numrecords 行结束标记。该变量定义的位置以及如何获取它是另一个问题。 这里不要使用静态数组,使用std::list,使用list::reverse_iterator(可以从[=12=中取值],迭代10次,除非iterator变得等于list::rend())来遍历最后 10 条记录,你的代码会简单得多(实际上,只有几行)

通常这是您可以使用 ifstream:

读取行的方式
#include <fstream>
#include <string>
#include <list>

using namespace std;

void readFile(const char* filename, list<string>& lines)
{
    lines.clear();
    ifstream file(filename);
    string s;
    while (getline(file, s))
        lines.push_back(s);
}

或阅读最后 10 行:

void readLast10(const char* filename, list<string>& lines)
{
    lines.clear();
    ifstream file(filename);
    string s;
    while (getline(file, s))
    {
        lines.push_back(s);
        if (line.size() > 10)
            lines.pop_front();
    }
}

然后你可以打印最后 10 行:

int main()
{
    list<string> lines;
    readFile(filename, lines);
    int n = 0;
    printf("read %d lines\n", lines.size());
    for (auto const it=lines.rbegin(); it!=lines.rend() && n<10; ++it, ++n)
        printf("line%2u:\t%s\n", lines.size()-n, it->c_str());
}

或类似的:

int main()
{
    list<string> lines;
    readLast10(filename, lines);
    int n = 0;
    cout << "read " << lines.size() << endl;
    for (const auto& line : lines)
        cout << l << endl;
}

请注意,getline 是逐行读取的,也就是说,如果您的文本不包含换行符,您的整个文件将被当作只有一个文件来读取。