reading from a file

ok lets say i have the following text

T 2 X F
X 2 Y G
Y 1 Z
Z 2 G 1
I 3 T G E
F 2 I E

looking at the first line t is the vertex 2 is the number of edges it has and X and F are the edges connecting to T.

So im suppose to implement an adjacency list which will read a file and store it in array called info which contains vertexname, outdegree, and adjcency list.

i really need help how to correctly read the file and store the values into an array using link list class i have

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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "slist.h"
#include "ll.C"
using namespace std;

void getData();
void printData();

const int SIZE = 10;

struct info
{
  char vertexName;
  int outDegree;
  slist adjacentOnes; // this comes from slist.h which is a link list class                                                   
};


int main()
{



}

//PURPOSE: GETS DATA FROM THE FILE                                                                                            
void getData()
{
  info table[SIZE];

  ifstream fin;
  fin.open("table.txt");
  while(!fin.eof())
    {
      for(int i = 0; i < SIZE;i++)
        {
          fin >> table[i].vertexName;
          fin >> table[i].outDegree;
          // this is where im kinda of lost, I have a slist class which is a      link list class                                  
          // i implemented and it works. It has a  member called addRear. addRear must be called                              
          // everytime a letter is read from the file.For how would i do this??   
          //                                             
        }
    }

}

void printData()
{


}



Last edited on
The next step after line 41 would be:

1
2
3
4
5
6
for(int j = 0; j < table[i].outDegree; ++j)
{
  char conn;
  fin >> conn;
  // addRear(conn) to your list
}


But you have another problem. This way you're reading just a single line. You may do the following after line 37:

1
2
3
4
5
6
7
std::string line;
std::geline(fin, line);
if(fin.good())
{
  std::stringstream ss(line);
  // from now on use ss instead of fin
}
ok now i have the following code, how could i use this information to use printData() and print the table
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void getData()

  ifstream fin("table.txt"); // opens the file
  string line; // declares line as a string

  // goes on a for loop and reads every variable, if variable is
  // a letter it calls addRear and appends it to the back of a list
  for(int i = 0; i<SIZE && getline(fin, line); i++)
    {
      istringstream S(line);
      S >> table[i].vertexName >> table[i].outDegree;
      char x;
      while(S >> x)
        table[i].adjacentOnes.addRear(x);
    }
  }
That depends on how that print is supposed to look like.
its suppose to look like the text file
like

T 2 X F
X 2 Y G
Y 1 Z
Z 2 G 1
I 3 T G E
F 2 I E
if the output looks like the input then code should be similar.

First you need a variable counter which counts the entries in your table.

Your function should look like this
1
2
int getData(info table[], int max_size); // parameter: the table and the size of the table / return: the number of entries
void printData(info table[], int size); // parameter: the table and the number of entries 
then you can use it in your 'main' like so:
1
2
3
4
5
6
7
8
9
int main()
{
  info table[SIZE];

  int size = getData(table, SIZE);
  printData(table, size);

  return 0;
}
Your print function might look somewhat like this:
1
2
3
4
5
6
7
8
9
void printData(info table[], int size)
{
  for(int i = 0; i<size; i++)
    {
      std::cout << table[i].vertexName << " " << table[i].outDegree;

      table[i].adjacentOnes.print(); // This would print recursively
    }
  }
Topic archived. No new replies allowed.