-1

I can't seem to get the program to call the second function. The program is supposed to open up a joke file, read it and display it for the user. Then close the file, open a second punchline file, seek the last line and read it to the user. I'm getting it to open the first file and display the joke but it doesn't do anything after that. Any idea what I'm doing wrong? Thank you in advance.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

// Function prototypes
void displayAllLines(ifstream &joke); // Display joke
void displayLastLine(ifstream &punchline); // Display punchline

int main()
{
    ifstream jokeFile, punchLineFile;

    // Open the joke file
    jokeFile.open("joke.txt", ios::in);

    // Make sure the file actually opens
    if (!jokeFile)
        cout << "Error opening file." << endl;

    // Call on function to display the joke
    displayAllLines(jokeFile);

    // Close the joke file
    jokeFile.close();

    // Open the punchline file
    punchLineFile.open("punchline.txt", ios::in);

    // Make sure the file actually opens
    if (!punchLineFile)
        cout << "Error obtaining the punchline, sorry :(." << endl;

    // Call on function to display punchline
    displayLastLine(punchLineFile);

    // Close the punchline file
    punchLineFile.close();

    system("pause");
    return 0;
}

// function to display the joke
void displayAllLines(ifstream &joke)
{
    string input;

    // Read an item from the file
    getline(joke, input);

    // Display the joke to the user
    while (joke)
    {
        cout << input << endl;
        getline(joke, input);
    }
}

// function to display the punchline
void displayLastLine(ifstream &punchline)
{
    string input;

    punchline.seekg(0L, ios::beg);  // Fast forward to the end of the file
    punchline.seekg('/n', ios::cur); // rewind the the new line character

    getline(punchline, input);  // Read the line
    cout << input << endl; // display the line

}
  • You can't "seem" to get the program to call the second function? That is just a guess. You should use a debugger and *find out* whether or not it calls the function. (I think it does, but that function doesn't do what you think -- as the answers given indicate.) – AAT Jun 30 '16 at 02:28

3 Answers3

1

seekg takes a offset in the file - you are passing it '/n' which is not an offset.

Because you used a forward slash (/), rather than a backslash (\), the compiler is treating '/n' as a Unicode or multibyte character sequence and moving forward 12142 bytes (at least in VS 2013), which is probably past the end of your file.

Also your comment says "Fast forward to the end of the file", but you are using ios:beg which is the beginning of the file.

The Dark
  • 8,453
  • 1
  • 16
  • 19
0
punchline.seekg(0L, ios::beg);  // Fast forward to the end of the file

No, it doesn't. This fast forwards to the beginning of the file, that's what "ios::beg" means.

punchline.seekg('/n', ios::cur); // rewind the the new line character

This does not rewind to the newline character, the comment notwithstanding. It does not rewind to the first newline character, it doesn't rewind it to the last newline character. seekg() always positions the get pointer at a fixed offset, as indicated. And, here, the fixed offset is utterly meaningless.

Your compiler is likely complaining about this line. Do not ignore your compiler's complaints, even if it still compiles the code despite them.

See this question for one possible algorithm to find the last line in the file.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
0

I faced this problem when i tried to call a particular function i but it everywhere in the program to be called from there but it doesn't until i discovered that there was an error with object code files "don't worry it is a simple problem". the solution: is simply just shutdown the environment you work on and open it again or rebuild your entire project. then you call will be executed successfully