Issues with file content outputting incorrectly

Hello Everyone,

I am new to c++ and am having trouble with a program that I am working on. The program involves outputting a joke and punchline to the user from associated text files. The joke file should display the entire contents of the file, while the punchline file should only display the last line of text (the prior lines are random characters that are not meant to be read. The problem I am experiencing is that the joke file content outputs on numerous lines, when it should all be on the same line. Here is the content of the joke file...

I started a band called 999 megabytes...

It is outputting as follows...

I
started
a
band
called
999
megabytes...

The punchline file is reading from the last row, but only displaying the last word in the row. Here is the contents of the file...

asfasdfasdfasdfsdf
asdfasdfsadfsadfsadf
asdfsadfsdfsdf
We haven't gotten a gig yet.

Here is what is outputting to the screen...

yet.

I have checked the file in notepad++ for any odd carriage return line feeds, but none are present that would explain this. Any assistance is tremendously appreciated, as I have been plugging away at this for hours to no avail.

Here is my code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*Include Section*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>

/*Namespace Section*/
using namespace std;

/*Function Prototypes Section*/
void displayAllLines(ifstream &inFile);
void displayLastLine(ifstream &infile);

/*Main section: this is the entry point of the program, which controls the flow of execution*/
int main()
{
	string file1;
	string file2;
	ifstream joke;
	ifstream punchline;
	char decision;
	char y;
	char n;
/*Beginning of program. Prompts user, asking them if they are ready to proceed. If yes, will display the joke\punchline. If no, 
ends program sequence*/

	cout << "*******************************************************************************" << endl;
	cout << setw(48) << "Punchline Program" << endl;
	cout << "*******************************************************************************" << endl;
	cout << endl;
	cout << "Welcome to the Punchline Program!" << endl;
	cout << "Are you ready to hear a joke? (y or n):  ";
	cin >> decision;

	if (decision == 'y')
	{
		cout << endl;
		cout << "Great! Prepare to laugh!" << endl;
		cout << endl;
	}
	else if (decision == 'n')

	{
		cout << endl;
		cout << "Ah, no sense of humor, I see. Time to make like a tree and leaf (queue rimshot)!" << endl;
		exit(EXIT_FAILURE);
	}
/*When user chooses "y", the following opens joke and punchline text files, outputting them to the user. The punchline file will
only display the last line of the file to the user*/
	joke.open("joke.txt");
	punchline.open("punchline.txt");
	cout << endl;
	displayAllLines(joke);
	displayLastLine(punchline);
	cout << endl;
	system("PAUSE");
}

void displayAllLines(ifstream &infile)
{
	string text;
	while (infile >> text)
	{
		cout << text << endl;
	}
}


void displayLastLine(ifstream &infile)
{
	string text;
	while (infile >> text);
	{
		cout << text << endl;
	}
}

In displayAllLines(): cout << text << endl; the endl puts a new line character after every word
For displayLastLine() you need to take the file pointer to the end of the file first and then keep going backwards from there on until you find the previous newline character, so everything in between this newline and the eof is your last line. You'd also have to keep checking that you've not gone past the start of the file while you're going backwards:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>

using namespace std;
void displayAllLines(ifstream &inFile);
void displayLastLine(ifstream &infile);
int main()
{
    ifstream file("F:\\test.txt");
   // displayAllLines(file);
    cout<<"Displaying last line only \n";
    displayLastLine(file);
}
void displayAllLines(ifstream &infile)
{
	while (infile)
	{
		string line;
		getline(infile, line);
		cout<<line<<"\n";
	}
}
void displayLastLine(ifstream &infile)
{
    if(infile)
    {
        //Go to the last character before EOF
        infile.seekg(-1, std::ios_base::end);
        char ch = ' ';
        while(ch != '\n')
        {
            infile.seekg(-2,std::ios_base::cur); //Two steps back, this means we
                                              //will NOT check the last character
            if((int)infile.tellg() <= 0)    //If passed the start of the file, this is the start of the line
            {
                infile.seekg(0);
                break;
            }
                infile.get(ch);                      //Check the next character
        }
            string lastLine;
            getline (infile, lastLine);
            cout<<lastLine<<"\n";
    }
}

PS: full disclosure: adapted displayLastLine() from http://stackoverflow.com/questions/11876290/c-fastest-way-to-read-only-last-line-of-text-file
Topic archived. No new replies allowed.