ifstream getting chars

I am writing a program to generate random numbers, output them to files based on if they are odd, even, or negative.I am then supposed to use an ifstream to get them back and display them to the terminal.

So far, I have 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<fstream>
using namespace std;

void main()
{
	ofstream eve, odd, neg;
	ifstream eve2, odd2, neg2;
	eve.open("H:\\Files\\EVENS.DAT");
	odd.open("H:\\Files\\ODDS.DAT");
	neg.open("H:\\Files\\NEGS.DAT");
	eve2.open("H:\\Files\\EVENS.DAT");
	odd2.open("H:\\Files\\ODDS.DAT");
	neg2.open("H:\\Files\\NEGS.DAT");

	
	srand(time(0));
	int a;
	for (int g=0; g<50; g++)
	{
		a=rand()%201-100;
		if(a<0)
			neg<<a<<" ";
		if (a%2)
			odd<<a<<" ";
		if(!(a%2))
			eve<<a<<" ";
	}
	
	cout<<"\nOdds: ";
	char next='\n';
	while(!odd2.eof())
	{
		next=odd2.get();
		cout<<next;
	}
	cout<<"\nEvens: ";
	next='\n';
	while(!eve2.eof())
	{
		next=eve2.get();
		cout<<next;
	}
	cout<<"\nNegatives: ";
		next='\n';
	while(!neg2.eof())		
	{
		next=neg2.get();
		cout<<next;			
	}
	cout<<"\n\n\n\n\n\n\n\n\n\n\n\n";
	system("PAUSE");
	eve.close();
	eve2.close();
	odd.close();
	odd2.close();
	neg.close();
	neg2.close();
}


The first part, using the ostream, operates correctly, and I get rewritten files every time I execute. But the input of chars does not work. Do you guys have any insight on this? I've tried both the .get() function and the extraction<< operators, both of which leave a blank terminal.

Thanks in advance.
Last edited on
Use int main.

1
2
3
eve2.open("H:\\Files\\EVENS.DAT");
odd2.open("H:\\Files\\ODDS.DAT");
neg2.open("H:\\Files\\NEGS.DAT");


You are opening the files before they are written, which means you are reading nothing. Open them after you write the data/close the output files.
Topic archived. No new replies allowed.