Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | Jul 2017 |
Last Sign in: | 273 Weeks Ago, 2 Days Ago |
Questions Answered: | 1850 |
Tutorials Posted: | 1850 |
Graduate in Biology and Nutrition, MBA Finance
Florida State University
Aug-2000 - Jul-2007
Ass. Relationship Manager
Penn-Florida
Mar-2009 - Feb-2016
I need help with this problem:
Â
"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."
Â
When debugging in Visual Studio, I get the following error:
Â
"Run-Time Check Failure #2 - Stack around the variable 'line' was corrupted."
Â
PLEASE HELP ME!
Â
I have included the joke.txt and punchline.txt for your use, as well as my code as follows:
Â
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstring>
Â
using namespace std;
Â
void functionJoke(fstream & infile);
void functionPunchLine(fstream & infile);
Â
Â
int main()
{
fstream jokeFile, punchLineFile;
Â
//open joke
jokeFile.open("joke.txt", ios::in);
//if joke file does not exist
if (jokeFile.fail())
{
cout << "Error opening joke filenn";
}
Â
//print joke
functionJoke(jokeFile);
jokeFile.close();
cout << "nnn";
Â
//open punch line
punchLineFile.open("punchline.txt", ios::in);
//if punch line does not exist
if (punchLineFile.fail())
{
cout << "Error opening punch line filenn";
}
Â
//print last line of file
functionPunchLine(punchLineFile);
//close
punchLineFile.close();
Â
system("pause");
Â
  return 0;
}
Â
//functionJoke
void functionJoke(fstream & infile)
{
//declare char
char ch;
//read char
infile.get(ch);
while (!infile.fail())
{
//if file is opened then print every char in file
cout << ch;
infile.get(ch);
}
Â
cout << "n";
}
Â
//implement punchLine function
void functionPunchLine(fstream & infile)
{
//declare variable
int i = 3;
char ch;
char line[30];
Â
//move to end
infile.seekg(0L, ios::end);
Â
//back three positions left
infile.seekg(-3L, ios::end);
Â
//get character
ch = infile.get();
Â
//until new line arrived
while (ch != 'n')
{
infile.seekg(-i, ios::end);
ch = infile.get();
i++;
}
Â
infile.getline(line, i);
cout << line;
cout << endl;
}
Attachments:Hel-----------lo -----------Sir-----------/Ma-----------dam----------- Â----------- -----------Tha-----------nk -----------you----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------acq-----------uis-----------iti-----------on -----------of -----------my -----------pos-----------ted----------- so-----------lut-----------ion-----------.Pl-----------eas-----------e p-----------ing----------- me----------- on----------- ch-----------at -----------I a-----------m Â----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I