Mageneto
Mageneto

Reputation: 23

C++ Trouble opening a file for output

So I am working on a program for class in which we have to open two different text files to retrieve the appropriate text to be displayed in the console. My code is not opening the file and keeps outputting the else statement ".txt file cannot be open". I've tried several different ways to open the file but with no luck. Any help here would be greatly appreciated.

//
//  main.cpp
//  PunchLine program
//  Page 896 Problem 3
//
//

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

int main()
{
    //File stream objects
    fstream inFile;
    string line;

    //Open joke file to read lines to console
    inFile.open("joke.txt", ios::in);

    if (inFile.is_open())
    {
        //Read lines from file to console
        while (getline(inFile, line))
        {
            cout << line << endl;
            inFile.close();
        }
    }    
    else 
        cout << "joke.txt file cannot be open.\n";

    //Open punchline file to read last line joke to console
    inFile.open("punchline.txt", ios::in);

    if (inFile.is_open())
    {
        //Read last line from file to console
        inFile.seekp(-52L, ios::end);
        getline(inFile, line);
    }    
    else 
        cout << "punchline.txt file cannot be open.\n";

    return 0;
}

Upvotes: 1

Views: 1318

Answers (5)

Dortimer
Dortimer

Reputation: 617

When declaring an input file use ifstream inFile; Also make sure the input file is in the same folder as your .exe

Edit: http://www.cplusplus.com/doc/tutorial/files/ also, this link should help with working with files.

Edit 2: I already posted this in a comment, but I'll just add it to the official answer: "Change your while loop as well. Instead of the if test, use while(inFile.is_open()) and then use your getline statement inside the loop. Because right now your code reads like while get this line from the file is true cout line. So it might not even be doing the while loop."

Upvotes: 2

HVar
HVar

Reputation: 140

I think you need to flush the screen. Once you have flushed and closed the stream. The next time you run an application it should open the file.

e.g.
inFile.flush();
inFile.close();

Upvotes: 0

mihai
mihai

Reputation: 4750

Check that your file exist. If it does, check whether you have the correct path when you open it (check if your .txt files are in the same directory as your .exe file, or specify the full path in your code). If yes, check if the files are read-only.

Upvotes: 0

Sourav Tiwari
Sourav Tiwari

Reputation: 39

use

if(!infile)
{
cout<<"cannot open file";
}

Upvotes: 0

Tom Swifty
Tom Swifty

Reputation: 2962

I don't think you should close the file inside the while loop. Otherwise, your file gets closed after only the first line is read in. Move the close statement outside the loop. Same for the second block.

if (inFile.is_open())
{
    //Read lines from file to console
    while (getline(inFile, line))
    {
        cout << line << endl;        
    }
    inFile.close();
}    
else 
    cout << "joke.txt file cannot be open.\n";

Upvotes: 1

Related Questions