So, i'm trying to write a program that can read data from a file. The file consists of integers, chars and doubles. And i have to be able to assign them to different variables for further calculation (this is actually just a part of a bigger program). I've searched around the web (here also), and i've seen different solutions. stringstream, vectors and whatnot. The one i got furthest with, was using "skipws" when reading. But i can't seem to read the integers properly.
I would prefer a solution that requires the use of as few methods (streams, gets etc) as possible. But if i need more, i can live with it.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace::std;
int main()
{
int j = 0, kill = 0, i;
char initials[10];
int results[10];
double avg;
ifstream read("P1.txt");
if (!read) //test to see if file can be opened.
{
cerr << "Error" << endl;
exit(1);
}
while(kill != 1) //kill = 1 same as eof. Just my weird way of coding.
{
switch (j)
{
case 0: //read the first line of data, which is the chars.
read >> skipws >> initials;
j++;
break;
case 1: //read the second line of data, which is 10 integers and whitespace
for (i=0;i<10;i++)
read >> skipws >> results[i];
j++;
break;
case 2: //read the last line which is a single double.
read >> skipws >> avg;
j++;
break;
case 3: //check for end of file.
if(read.eof() == 0)
kill = 1;
break;
}
}
//the 3 lines below are just writing the contents of the file to the prompt.
//It's to check that values are stored correctly for further use.
cout << initials << endl;
cout << results << endl;
cout << avg << endl;
return 0;
}
I know exactly what y input file, "P1.txt", will look like, because i create it myself in another part of the program. It looks like this:
ccs
2 4 5 3 1 4 6 1 1 1
2.8
Those 3 lines gives me the initials of a player, his results of 10 games and his average score. In short, the data of one player. In the final program, i need to be able to read values for maybe 10 players.
I'm expecting the 3 cout lines in the end to show this:
ccs
2453146111
2.8
Now, this is as far as i have gotten. If i declare results[10] as char i can get this in my console:
ccs
2453146111ccs
2.8
If i declare it as int i get this
ccs
0x7fff5fbff6c0
2.8
Obviously, i can see that values aren't stored properly and something is out of hand, but i need another persons eyes on this. I'm out of clues.
I know this is a messy way to do it and that it can be done in less space, but i'm new to C++, so this made sense to me, and made it fairly easy for me to locate my mistakes, until now.
I tried to explain it as well as i could. This is my first post here, so please tell me if you need more info.
Thanks in advance!! Chris.