read file into array

ok lets say i have the following text file

A 2 B F
B 2 C G
C 1 H
H 2 G 1
I 3 A G E
F 2 I E

and the following struct
1
2
3
4
5
6
struct info
{
  char vertexName;
  int outDegree;
  slist adjacentOnes; 
};


i want to read the file and store it in a table array
for example the first line: "a" will be the vertexname, 2 the outdegree and b and f the adjecent sides.

i have the following code but im not sure if its right, anybody can point me in the right direction?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void graph::filloutTable()
{
  int x;
  char vertexName;
  int outDegree;
  int visit;
  slist adjacentOnes;

  info table[SIZE];
  ifstream fin;
  fin.open("table.txt"); // opens the file                                        
  while(!fin.eof()) // keeps reading until end of file.                           
    {
      for(int i = 0; i < SIZE; i++)
        {
          fin >> table[i].vertexName;
          fin >> table[i].outDegree;
          fin >> x;
          table[i].adjacentOnes.addRear(x); // note addRear is a function from a different file, it is a link list function. 
        }
    }
  fin.close();



Last edited on
a couple issues i see here:
1) x is an int and you're tying to store a char in it
2) there can be any number of chars after you read in the vertexName and outDegree

i think you can figure out the first part on your own. as for the second part, what i would do is after getting outDegree, say something like:
1
2
3
4
5
6
char x;
do {
   x = fin.get();
   if (x != ' ' && x != '\n')
      table[i].adjacentOnes.addRear(x);
} while (x != '\n');


i didn't put a whole lot of thought into this, but the general idea is to loop until you reach the new line character and for all valid characters, append them to your list. then once you've reached the end of the line you can start with your vertexName on the next line
Last edited on
Topic archived. No new replies allowed.