Iostream Problem

Hi,

I'm learning ifstream/ostream staff. Once I started I already got a problem, very strange one.
This is my code
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
#include <iostream>
#include <fstream>
#include <string> 

using namespace std;

int main ()
{
	ifstream infile; 
	infile.open("nombres.rtf");

	//check for error

	if (infile.fail() )
	{
		cerr << "Error opening file" << endl;
		exit(1); 
	}

	int x, y;

	infile >> x >> y; 

	cout << "Num 1: " << x << endl;

	cout << "Num 2: " << y << endl;
	return 0;
}


Normally, there is no problem. In the file nombres.rtf, I wrote 8 on first line, and 9 on second line.
But it tells me :
1
2
Num 1: 0
Num 2: 0


Isn't that weird??
Plus, it's only a code I copied from a tutorial !

I would appreciate some help ! :)
I just posted about the same tutorial. Mine compiles, but doesn't run properly. Are you using Visual Studio?
RTF files aren't really suitable for raw ifstream usage.
Use txt's.
S G H : thx for your response, I already tried this before posting, but it doesn't work either...

bostock11 : I'm on mac ! And I don't use IDE !
1) What happens when you use a .txt file as S G H suggests?
2) Post both the line that shows you using a txt file, and also show us what is in the txt file.
Last edited on
What does infile.is_open() return?
Hi thx tscott8706 and S G H

I wrote this code to test within file.is_open()
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
30
31
32
33
#include <iostream>
#include <fstream>
#include <string> 

using namespace std;

int main ()
{
	ifstream infile; 
	infile.open("nombres.txt");

	if (infile.is_open() )
	{
		cout << "ouvert" << endl;
	}

	//check for error

	if (infile.fail() )
	{
		cerr << "Error opening file" << endl;
		exit(1); 
	}

	int x, y;

	infile >> x >> y; 

	cout << "Num 1: " << x << endl;

	cout << "Num 2: " << y << endl;
	return 0;
}


This is what the .txt file contains
1
2
8
9


And this is what that return

1
2
3
4
macbookdeapple:Desktop apple$ ./a.out
open
Num 1: 0
Num 2: 0
1
2
3
4
5
6
7
8
9
10
11
#include <fstream>
#include <iostream>

int main()
{
    int a, b;
    if(std::ifstream("numbers.txt")>>a>>b)
        std::cout<<a<<' '<<b<<std::endl;
    else
        std::cout<<"Error"<<std::endl;
}


What does this say?
Error
Is the txt file encoded in some weird format?
"Text format document"
Could you show your current code?

Edit: Make sure you get your text files name right

Edit 2: Im not sure how it works when you're not using an IDE but, make sure you're file is stored where its supposed to be, so the compiler can actualy find it.
Last edited on
TarikNeaj, thx, yes I already checked all this

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
int main ()
{
  
	ifstream infile; 
	infile.open("nombres.txt");

	if (infile.is_open() )
	{
		cout << "open" << endl;
	}

	//check for error

	if (infile.fail() )
	{
		cerr << "Error opening file" << endl;
		exit(1); 
	}

	int x, y;

	infile >> x >> y; 

	cout << "Num 1: " << x << endl;

	cout << "Num 2: " << y << endl;
	return 0;

}

Man thats real weird... Works perfectly fine for me...
... yes ... thank you to have checked it all
Open the TXT file in a plain text editor like Notepad or Notepad++, not a rich text editor.
I think it is down to the path of the input file.
Is the data file in the same directory with the executable?
To find out what directory your program is looking for files in, run this code:
1
2
3
4
5
6
#include <fstream>

int main()
{
    std::ofstream("use-this-directory.txt") << "Use this directory for input files.";
}
Then just look for the file.

Some IDEs (especially Visual Studio) make this really confusing.
Last edited on
Topic archived. No new replies allowed.