reading and displaying txt file

Hello, I'm trying to have my program read a txt file that's in the same directory as the exe in visual basic. All I get is a blank screen when I run the program (blank black console screen). The program compiles and and runs, but it displays nothing. It should output the first letter, then the next, until the end of the txt file. At least, that's what I want it to do.

#include "stdafx.h"
#include "fstream"
#include "iostream"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile;
infile.open("p4in.txt");
while(!infile.eof())
{
char c = infile.get();
cout << c << endl;
}
}

Looking at your code, it seems that you haven't done any interactions with the file before entering your loop. In order for your loop to work, you'll need to have at least one interaction with the file so that the end-of-file bit is properly set.

So you'd need to alter your code to something like this:

#include "stdafx.h"
#include "fstream"
#include "iostream"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile;
infile.open("p4in.txt");
char c = infile.get();
while(!infile.eof())
{
cout << c << endl;
c = infile.get();
}
}

This way, if there are no more characters in your file, the infile.get() would set off the end-of-file bit, and end the loop in the next iteration.
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
#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char * argv[])
{
    ifstream infile;
    infile.open("p4in.txt");
    if (!infile.good())
    {
        cout << "Input file not open" << endl;
        return 0;
    }

    char c = infile.get();
    while(!infile.eof())
    {
        // cout << c << endl;
        cout << c;

        c = infile.get();
    }

    return 0;
}

You need to check that the file was successfully opened.

Secondly, doing this is a bad idea:
1
2
    char c = infile.get();
    cout << c << endl;

Here the second line will attempt to display character c regardless of whether or not the read was successful. My code above makes sure the eof() test is always done before the cout statement is attempted.

Edit: I see that mukomo has said much the same thing.
Last edited on
I don't think I'm actually opening the file. I have the program open in visual studio 2012. The text file is in the same folder as the .sln. This doesn't show anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdafx.h"
#include "fstream"
#include "iostream"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile;
infile.open("p4in.txt");
char c = infile.get();

}
Well, with the code you just provided there's no cout statement, so there's nothing to display.

But if you think you're not opening the file, then you can try what Chervil showed and include this between lines 10 and 11:

1
2
3
4
5
if (!infile.good())
    {
        cout << "Input file not open" << endl;
        return 1;
    }


I changed it to return 1 so we can tell if there's a difference between this and normal execution in case there's something wrong with your console.
Last edited on
Rather than being not quite sure whether the file is open or not, test either infile.good() or infile.is_open() immediately after opening the file.
Oh, yah I didn't put the cout part. Anyway, I did this and it said it's definitely not reading the txt file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include "fstream"
#include "iostream"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile;
infile.open("p4in.txt");
if (!infile.good())
    {
        cout << "Input file not open" << endl;
        return 0;
    }

}
Most likely the file "p4in.txt" is in a different location to the executable program.
My compiler uses separate folders for the release and debugging versions of the program, so some care is needed.]

You need to either place the file in the same folder as the .exe, or specify the full path to the file, for example "c:\\temp\\p4in.txt".

Well, there is another solution, using relative paths, in order to use that, you'd need to know where the .exe resides relative to the .txt file. Then you might use something like "..\\..\\p4in.txt", where .. means one level higher in the folder hierarchy.
Last edited on
Ok, I pasted it in every folder and subfolder in the directory of the program and it appears to have worked. I'll have to figure out the specifics later. Thanks
Ok yah, so I worked on it some more and got this. It displays all the text of the file, one letter at a time. Thanks for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stdafx.h"
#include <iostream>
#include "ctype.h"
#include <fstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	
	
	ifstream infile;
	infile.open("p4in.txt");
	while(!infile.eof())
	{
		char x = infile.get();
		cout << x;
	}
		return 0;

	
}
Wait, how is that any different than the original you showed us? Other than removing the endl on line 16.
The code is flawed because this cout << x; is executed without first testing whether the read was successful.

If you change line 16 to this:
 
        cout << '[' << x << ']';

you would expect to see every character of the file enclosed in square brackets.
This is my input file:
A


and this is the corresponding output:
[A][ ]

Notice the extra output.
This is solved. I just didn't have the file in the right spot in visual studio, so that my program could read it.
Topic archived. No new replies allowed.