In C I need to read from a text file namestxt The names appe
In C++, I need to read from a text file (names.txt). The names appear as thus:
(This is just a snippet, there are over 4400 names in this txt file)
Ambrose 358 394 551 711 869 0 0 0 0 0 0
Amelia 152 168 211 251 293 386 431 411 254 207 210
America 943 0 966 880 0 0 0 0 0 0 731
Americo 946 669 674 0 0 0 0 0 0 0 0
Ami 0 0 0 0 0 0 0 589 915 0 0
Amie 844 945 942 0 0 0 819 313 374 0 0
Amina 0 0 0 0 0 0 0 0 0 0 964
Amir 0 0 0 0 0 0 0 978 683 545 415
Amira 0 0 0 0 0 0 0 0 0 0 836
Amiya 0 0 0 0 0 0 0 0 0 0 939
Amos 218 222 258 323 402 439 586 578 712 860 0
Amparo 0 851 695 741 994 0 0 0 0 0 0
Amy 196 244 284 356 331 136 35 2 16 61 107
Each name is followed by 11 integers that represent something.
Using:
struct Name
{
char name[NAME_LEN];
int rank[NUM_RANKS];
};
Where NAME_LEN is 21 and NUM_RANKS is 11...
How do I read this file into an array of structs?
I have this and it isn\'t working...SIZE is the number of names in the text file.
void loadNames( Name list[SIZE] )
{
int i = 0;
fstream theFile;
theFile.open(\"names.txt\");
while (i < SIZE)
{
theFile >> list[i].name;
for (int k = 0; k < 11; k++)
{
theFile >> list[i].rank[k];
}
i++;
}
theFile.close();
}
Solution
struct Name
{
char name[NAME_LEN];
int rank[NUM_RANKS];
};
Where NAME_LEN is 21 and NUM_RANKS is 11...
How do I read this file into an array of structs?
I have this and it isn\'t working...SIZE is the number of names in the text file.
void loadNames( Name list[SIZE] )
{
int i = 0;
ifstream theFile;
theFile.open(\"names.txt\");
while (!thefile.eof)
{
getline(thefile, String);
cout<<String;
for (int k = 0; k < 11; k++)
{
theFile >> list[i].rank[k];
}
}
theFile.close();
}