Punch Line case

Write a program that reads and prints a joke and its punch line from two different files. The first file contains a joke, but not its punch line. The second file has the punch line as its last line, preceded by “garbage.” The main function of your program should open the two files and then call two functions, passing each one the file it needs. The first function should read and display each line in the file it is passed (the joke file). The second function should display only the last line of the file it is passed (the punch line file). It should find this line by seeking to the end of the file and then backing up to the beginning of the last line. Data to test your program can be found in the joke.txt and punchline.txt files.

Hello Seniors, I have mentioned problem of my code, kindly guide me.
My first part of the program (function "Jokedata") works well. but I have problem in 2nd part of the program (function "Punchlinedata"). It doesn't show last line of the file. Code is given below...

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

int main()
{
	fstream joke, punch;
	joke.open("name.txt", ios::in);
	if(!joke.fail())
	{
		jokedata(joke);
	}
	else
	{
		cout<<"whole  1 opening error"<<endl;
	}
	
	joke.close();
	
	punch.open("number.txt", ios::in);
	if(!punch.fail())
	{
		punchlinedata(punch);
	}
	
	joke.close();   
	punch.close();
}

//function for first file, it is used for getting all the data from the joke file.
void jokedata(fstream &file)
{
	string str;
	while(getline(file, str))
	{
		cout<<str<<endl;
	}
}

//function for getting last line of the 2nd file punch object.
void punchlinedata(fstream &file)
{
	string str;
	char ch;
	file.clear();
	file.seekg(0L, ios::end);
	file.get(ch);
	while(ch!='\n')
	{
		file.seekg(-1L, ios::cur);
		file.get(ch);
	}
	getline(file, str);
	cout<<str<<endl;
}
Last edited on
If you're trying to loop until you reach the end of the file, do this instead:

while(std::getline(file, str))

If you're loop condition is checking for '\n' - then you'll end up cutting the loop early if there's a blank line in the .txt
I just changed the little bit code of the second function. It is working now but can anyone suggest elegent way to solve it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//function for getting last line of the 2nd file punch object.
void punchlinedata(fstream &file)
{
	string str;
	char ch;
	long position=-1;
	
	file.clear();
	file.seekg(0L, ios::end);
	while(file.seekg(position, ios::cur))
	{
		position--;
		file.get(ch);
		if(ch=='\n')
		{
			getline(file, str);
			cout<<str<<endl;
		}
	}
}




Regards
Yehya
Last edited on
Do you have to match the joke with the correct punchline?
Remember that when use use ios::cur, it is relative to the CURRENT position in the file. You should be seeking from ios::end EVERY time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void print_punchline(fstream& file)
{
  long position = 0;
  while (file.seekg( --position, ios::end ))
  {
    if (file.get() == '\n')
    {
      std::string str;
      getline(file, str);
      cout << str << endl;
      return;
    }
  }
}

;-)
Thankyou so much Zapshe and Duthomhas

Dear Zapshe, Actually Joke is another file that is running accurately. Punchline is another file that is running separately and I want to get last line of that file. I got your point for mentioning the while(std::getline(file, str)).
Thankyou so much.


Dear Duthomhas,

Thank you so much for elaborating code in detail.

Regards
Topic archived. No new replies allowed.