Trying to open a .txt file for decoding

Hey very new to C++,
so we have to decode this message for an assignment in class but when i try to open the original message it just says the name of the file and then the program ends. Greatly appreciate any advice.


#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;

int main ()
{
ifstream SecretSociety;
SecretSociety.open("encoded_quotes.txt");
//remove all text except “encoded_quotes.txt”

if ( SecretSociety.fail() )
{
cout <<"encoded_quotes.txt" << endl;
exit (-1);

char ch;
SecretSociety >> ch;
while ( SecretSociety )
{
cout << ch << endl;
SecretSociety >> ch;
}
SecretSociety.close ();
return 0;
}
}
Last edited on
It seems this is your homework. You already said what it does, so what's the question ?
when the program runs, it only displays the file name not the content inside the file
closed account (E0p9LyTq)
Checking the filestreams failbit is not going to do what you seem to expect it to indicate. After trying to open a file you should check if the filestream object is actually open using the is_open() filestream method:
http://www.cplusplus.com/reference/fstream/ifstream/is_open/

And PLEASE use code tags, it makes it easier to read your code. You can go back and edit your post to add code tags.
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
There are two separate problems.

The first, there is a logic error in the code. That is made clearer if we add some indentation for legibility. Unchanged except formatting:
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
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;

int main ()
{
    ifstream SecretSociety;
    SecretSociety.open("encoded_quotes.txt");
    //remove all text except “encoded_quotes.txt”
    
    if ( SecretSociety.fail() )
    {
        cout <<"encoded_quotes.txt" << endl;
        exit (-1);
        
        char ch;
        SecretSociety >> ch;
        while ( SecretSociety )
        {
        cout << ch << endl;
        SecretSociety >> ch;
        }
        SecretSociety.close ();
        return 0;
    }
} 


If the file was not opened successfully, then at line 17 the exit function is called, ending the program.

On the other hand, if the file was opened successfully, none of the statements in lines 15 to 28 will be executed at all. The program simply ends.

Either way, the useful part of the program, lines 19 to 25 can never be executed.

Action
1. Remove the closing brace at line 28. Instead, insert a closing brace at line 18.
2. Check the file path is correct. The file will fail to open usually because the program is looking in a different directory to where the file is located. This depends on how the program is run, usually, place the file in the same folder as the .exe file. This may not be the same as the source code.

As for checking the fail bit, that should be ok as it stands. Checking is_open() would also work.

I'd also recommend changing the text at line 16 to something like "Error, could not open file: " and then the file name, so it is more meaningful.

Last edited on
Topic archived. No new replies allowed.