1

I'm trying to read the contents of a file object into an array of strings, but whatever I try nothing is displayed when I print the contents of the array. Specifically, I want to print the last ten lines of the text file by passing them into the array, and using a for loop on the array.

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

The file(s) are simply large blocks of text, including spaces. The file looks like:

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.

I want to read each line into it's own element of a string array.

I do have another function that successfully uses getline() to display each line of the text file 10 lines at a time.

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();
}
5
  • In which format you're storing data in file? Commented Apr 16, 2017 at 8:10
  • Oh, right. Each line of the file(s) are lines of text including spaces. Something like: 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. Commented Apr 16, 2017 at 8:11
  • I don't think the following is the error but you open the file twice. First using the constructor ifstream ifile(filename); and secondly using the function ifile.open(filename);. Commented Apr 16, 2017 at 8:13
  • Ok. Thank you, I'll fix that. Commented Apr 16, 2017 at 8:16
  • seems like an ideal scenario for firing up the debugger Commented Apr 16, 2017 at 8:58

2 Answers 2

1

Usually this is how you could read lines using 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);
}

or to read last 10 lines:

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

and then you can print last 10 lines:

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

or like that:

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

Note that getline reads line by line, that is, if your text doesn't contain line breaks your entire file will be read as if it was only one file.

Sign up to request clarification or add additional context in comments.

5 Comments

Alright, thank you for that. So, without line breaks in the file itself, I can't just read each line into a regular array?
@johnbodhi Without line breaks you do not have lines, but only one very long line.
I see. I do have another function here that successfully displays each line 10 lines at a time using getline though. I don't know how to display it in these comments.
You can edit your original question and add it there.
Not sure what I did, but it is working now! Haha. I will learn more about lists for the future per everyone's advice. Thank you.
0

It's possible that your file doesn't contain proper line end markers like your program expects but your code assumes that file just got numrecords end-of-line markers. Where that variable defined and how you get it is other question. Do not use static array here, use std::list, use list::reverse_iterator (can get value from list::rbegin(), iterate 10 times unless iterator becomes equal to list::rend()) to iterate through last 10 records, your code would be far simpler (actually, just few lines)

1 Comment

Thanks. I'm not very familiar with lists yet, but I'll see what I can do. The numrecords is a private member variable that is defined once each line of the file has been read in another member function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.