954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

program to print last 10 lines of text and ofstream problem

Hello guys!

I have to create a program that prints the last 10 lines of a text or all of it if the number of the lines is <=10. The input will be a file that is unknown to me. This is what I've done so far.

#include <iostream>
#include <fstream>
#include <string>
#define LinesToPrint 10

using namespace std;

int main()
{
        ofstream outputfile;
        string line;
        int lines=0,i=0;
        char c;
        outputfile.open("newfile.txt");
        while (getline(cin,line))   //I copy each line of the file to the file I created
        {
                outputfile<<line<<endl;  //I put endl because I don't know if '\n' is
                lines++;                 //included in getline
        }
        if (lines==0)
        {
                cout<<"Den upirxe eisodos!"<<endl;  //if the file was empty I print a
        }                                            //message saying that
        else if (lines<=10)
        {
                cout<<outputfile<<endl;  //if there are 10 lines or less I print it
        }
        else
        {
                while (outputfile.get(c))  //my problem is here!!! I also tried to use
                {                          //getline but I had the same problem as now
                        if ((c=='\n')&&(i!=lines-LinesToPrint+1))
                        {
                                i++;
                        }
                        if (i==lines-LinesToPrint)
                        {
                                i++;
                                continue;
                        }
                        if (i>lines-LinesToPrint)
                        {
                                cout<<c;
                        }
                }
        }
        outputfile.close();
        return 0;
}


if my program entered else, I would want to print the last 10 lines but it seems I can't use ofstream file as input. the error I get when I try to compile is this:
./seira2/ask1.cpp: In function 'int main()':
./seira2/ask1.cpp:30: error: 'struct std::ofstream' has no member named 'get'

can you please help me with that? I am looking for hours to find a solution but I have a deadline and I can't fix it!

Thank you in advance!

lmytilin
Newbie Poster
18 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Make an array of 10 strings.
Put each line read into this array.
After you've read 10 lines, start overwriting the strings from string[0].

In other words:
read line 1 --> load string[0]
read line 2 --> load string[1]
read line 3 --> load string[2]
read line 4 --> load string[3]
read line 5 --> load string[4]
read line 6 --> load string[5]
read line 7 --> load string[6]
read line 8 --> load string[7]
read line 9 --> load string[8]
read line 10 --> load string[9]
read line 11 --> load string[0]
read line 12 --> load string[1]
etc...

To output, skip to the next string (the one after the last one written) and start writing all the strings in order.

But -- what happens if there are only 7 lines in the file? Yours to figure out.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

While it is not the only problem with the code, the main issue is that you are using an output file for input. You will want to change the program thusly:

#include <iostream>
#include <fstream>
#include <string>

const int LinesToPrint = 10;

using namespace std;

int main()
{
    ifstream inputfile;
    string line;
    int lines=0,i=0;
    char c;
    inputfile.open("newfile.txt");
    while (getline(cin,line))   //I copy each line of the file to the file I created
    {
        inputfile >>line;  //I put endl because I don't know if '\n' is
        lines++;                 //included in getline
    }
    if (lines==0)
    {
        cout<<"Den upirxe eisodos!"<<endl;  //if the file was empty I print a
    }                                            //message saying that
    else if (lines<=10)
    {
        cout<<inputfile<<endl;  //if there are 10 lines or less I print it
    }
    else
    {
        while (inputfile.get(c))  //my problem is here!!! I also tried to use
        {
            //getline but I had the same problem as now
            if ((c=='\n')&&(i!=lines-LinesToPrint+1))
            {
                i++;
            }
            if (i==lines-LinesToPrint)
            {
                i++;
                continue;
            }
            if (i>lines-LinesToPrint)
            {
                cout<<c;
            }
        }
    }
    inputfile.close();
    return 0;
}


This doesn't actually work, as of now, but it will at least compile. If you need more help debugging this, let us know.

Schol-R-LEA
Posting Pro
556 posts since Oct 2010
Reputation Points: 254
Solved Threads: 85
 

As Schoil-R-LEA mentioned above correctly, you need an input file stream to read from a file. ofstream is used for output. Also note that your second while loop will never print anything if your file contains less than 10 lines. The structure of the programm should rather be modified.

Y.G. Tha s egrafa sta ellinika alla exoun kanona... :) an xreiasteis kati allo steile minima

mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11
 

As Schoil-R-LEA mentioned above correctly, you need an input file stream to read from a file. ofstream is used for output. Also note that your second while loop will never print anything if your file contains less than 10 lines. The structure of the programm should rather be modified.

Y.G. Tha s egrafa sta ellinika alla exoun kanona... :) an xreiasteis kati allo steile minima


wouldn't it be printed in else if? :/ I considered copying the ofstream to another file but that would be for output only as well, right? I'll try what WaltP said.. but I'll have to use dynamic memory in that case and we just mentioned a few things about pointers in class.

thanks for your help

euxaristw polu ;)

lmytilin
Newbie Poster
18 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 
I'll try what WaltP said.. but I'll have to use dynamic memory in that case and we just mentioned a few things about pointers in class.


Why? Your analysis is faulty.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: