Cannot Get Text From .TXT File.

Heya, guys. I've been trying to make a command-line RPG game for a little bit, and I've gotten to a point where I ask the user if they want to learn more about the Family History.

I decided to bring the long output from a text file, in order to keep my main function clean.

Here is the code from the If Statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// ***************************************************************************************************************
        // Initiates Family History as well as cleared screen.

        if (historyClan == 'Y'){
            system("CLS");
            cout << "You have chosen to be told the brief history of the Sinclair Clan...";
            cout << "\n\n" << endl;
            ifstream inFile;
            inFile.open ("FamilyHistory.txt");

                if (!inFile){
                    cout << "Unable to find text file!";
                }

        }


Yes, I have included <ifstream>.

The only thing that outputs in the Console is the:
You have chosen to be told the brief history of the Sinclair Clan...

Here is the output from the text file:

1
2
3
cout << "\n\n\n\t When " << userName << " was born, his mother and father had moved to Annville.  << endl;
cout << "His mother worked as a musician, playing for the King, and his father worked as\n" << endl;
cout << "a blacksmith, creating various weapons for the King's men." << endl; 


It's not much, but it's not yet finished. (I wanted to test it out, first)
Last edited on
I haven't worked much with file input and output but does this code output the text?
inFile.open("FamilyHistory.txt"
or are you missing something?

are you getting compiler errors?
It doesn't output anything. Is it just because I am only opening the file, and not actually telling it to display it's text?

:)))
inFile.open("FamilyHistory.txt", ios::in);
EDIT: That guy probably knows what he's doing ^^

That's the only answer i see.

not sure if this would do it, but try something along the lines of
cout << inFile.open("FamilyHistory.txt");
Last edited on
nano511, that would either be an ambiguous call or just print out if the stream is OK or not (0 or 1). In order to actually print the file, you need to do something like this:
1
2
3
4
5
6
ifstream InFile ("FamilyHistory.txt");
int ch;
while((ch = InFile.get()) != EOF)
{
  cout << char(ch);
}
Alright, so I moved my code up to my upstairs computer, and I recreated a .txt file with the same name, and the same code.

I entered this:

inFile.open("FamilyHistory.txt", ios::in);

And now it comes up as:

Unable to find text file!

Why is it not being found? Do I have to be more specific with the location?
Did you make sure the file was in the same directory as your project?
Hey, thanks, LB! That code worked, and I did move my .txt file to the directory.

Do you mind me asking how your small snippet works? I would really like to understand that syntax in order for me to understand and use it again. :)
Ok, I'll go through it line by line:

ifstream InFile ("FamilyHistory.txt");
This constructs a new std::ifstream called InFile, and gives its constructor the path to the file to open. This constructor has an optional second parameter that, if not specified, automatically uses ios::in.
http://www.cplusplus.com/reference/iostream/ifstream/ifstream/


int ch;
This just makes an integer for the std::ifstream::get() function below.


while((ch = InFile.get()) != EOF)
First, we call the get() member function of InFile. This returns an integer, so we store it in the integer from earlier. The reason it returns an integer is because some of the OS flags (such as End Of File) are not in the character range 0 to 255 or -128 to 127 - and with good reason, because then we'd have no way to tell apart file data from a special system flag. After the assignment, we compare the value of the integer to the EOF flag so that the loop keeps going until we reach the End of File system flag.
http://www.cplusplus.com/reference/iostream/istream/get/
There is also a specific function that can tell you if you've reached the end of the file, but I did not use it here: http://www.cplusplus.com/reference/iostream/ios/eof/

1
2
3
{
  cout << char(ch);
}

Inside the while loop, we cast the integer to a character so that when we print it out it will print out characters like "hello" and not numbers.

Also, the destructor of std::ifstream automatically closes the file for us, so we don't have to do that.
Last edited on
Topic archived. No new replies allowed.