How to read/print from a data file.

I am having a problem reading a set of data from notepad that looks like this:
300 450 500 210
510 600 750 400
627 100 420 430
530 621 730 530
200 050 058 200
100 082 920 290

this is what i have so far:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
float shift;
ifstream inFile;
inFile.open("factory.dat");
inFile >> shift >>shift;

if (!inFile)
{
cout << "Unable to open file";
exit(1); // terminate with error
}



cout << shift << shift;
inFile.close();
return 0;
}

i only get this
OUTPUT: 450450.

I need to print the data exactly as shown.
Help would be greatly appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main() 
{
float shift;
ifstream inFile;
inFile.open("factory.dat");


if (!inFile) 
{
cout << "Unable to open file";
exit(1); // terminate with error
}


while (inFile >> shift)
	cout << shift << endl;

inFile.close();
return 0;
}
it is supposed to be displayed how i showed it here, not in a long list.

I don't know if i need to load an array or not to do this.
in this way
You can read line by line

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main() 
{
string shift;
ifstream infile;
infile.open("factory.dat");


if (!infile) 
{
cout << "unable to open file";
exit(1); // terminate with error
}


while (getline(infile, shift))
	cout << shift << endl;

infile.close();
return 0;
}
in this way
You can write to the array


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
int main() 
{
float shift[1000];
ifstream infile;
infile.open("factory.dat");
int k = 0;


if (!infile) 
{
cout << "unable to open file";
exit(1); // terminate with error
}


while (infile >> shift[k])
	k++;

for (int i = 0; i < k; i++)
	cout << shift[i];
cout << endl;

infile.close();
return 0;
}
thanks man, the first one worked, i thought i was going to need an
array but i see that i don't
It does not matter :)
Topic archived. No new replies allowed.