0

I'm trying to read a specifically formatted text file into a 2d array however I don't think I am doing it right. This is what I have so far:

int main()
{
   int start;
   int end;
   int ch = 0;
   int lines = 0;
   int i;
   int j;

   FILE *fp;
   fp = fopen ("file.txt", "r");
   rewind(fp);
   while(!feof(fp))
   {
       ch = fgetc(fp);
       if (ch == '\n')
       lines++;
   }

   int graph[lines][lines];
   memset(graph, 0, sizeof lines);

   //I don't think I'm doing the matrix populating correct. PLEASE HELP!
   for (i = 0; i < lines; i++)
   {
       for (j = 0; j < lines; j++)
       {
           int node;
           int edge;
           fscanf(fp, "%d,%d", &graph[&node][&edge]);
       }
   }
}

The while loop and everything in it counts how many lines there are in the text file and the so I set the matrix to the number of lines there are in the file.

This is my text file

2,2 4,6
1,2 3,3 4,8 5,5
2,3 5,7
1,6 2,8 5,9
2,5 3,7 4,9

This is an adjacency list so the first line means node 2 with edge weight 2 and node 4 with edge weight 6 are adjacent to node 1 (first line of text file) so on and so forth.

My problem is, is that I don't know how to put the information from the text file into a matrix. Any advice would help!

  • Does the last line in the file have a `\n` at the end? If not, then you should have it so that you have enough memory for scanning data from the last line. – Spikatrix Apr 17 '15 at 13:55
  • 1
    What is `&graph[[]` in `fscanf(fp, "%d,%d", &graph[[]);`? Did you mean `&graph[i][j]`? – Spikatrix Apr 17 '15 at 13:56
  • 1
    Also, read [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Spikatrix Apr 17 '15 at 13:56
  • Yeah I don't know what I'm doing inside the for loops. I just looked up stuff and hoped for the best. The while loop works fine. It prints out exactly what I want. – Don'tRoastMePlease Apr 17 '15 at 14:13
  • I wasn't very clear on what exactly my problem was so I edited the bottom part to clearly state it. The while loop was never my problem. – Don'tRoastMePlease Apr 17 '15 at 14:17
  • Think about what you are doing (at least what the code implies you wanted to do): first you want to count the number of lines by reading once through the file and count the number of `'\n'`s. Then you want to read the file again line by line into an array. There are (at least) two mistakes here: first, after counting the lines, your file pointer is where? Yes, at EOF. You can't read behind EOF. Second, you declare an array with size of on a variable unknown at compile time. Not possible - array size in C must be constant. Last: you are very generous with &'s. Not enough space here for all erro – mfro Apr 17 '15 at 14:45

1 Answers1

0

some advices for you:

  • Seems that you are learning C and this is your assignment thus would better in futher you solve these own your own. As there is some purpose in every assignment

  • Stop using Turbo C/C++ in Learning C as it is old fashion and currently doesn't meet the coding requirements.

Many Errors

  1. rewind(fp) - is not required after you opened the file but required when you reache the end of the file to reach at top of the file. Thus, the rewind(fp) would be as under:

    fp = fopen ("file.txt", "r");
    while(!feof(fp)){
       ch = fgetc(fp);
       if (ch == '\n')
       lines++;
    }
    rewind(fp);
    
  2. Now Reading the values and filling the 2D array as under:

     while (!feof(fp)) {
         int node;
         int edge;
         char c;
         int lineno=0;
    
         fscanf(fp, "%d,%d%c", &node, &edge, &c);
         graph[node][edge]=lineno;
         if ( c == '\n' ){
              lineno++;
         }
     }
    
Vineet1982
  • 7,730
  • 4
  • 32
  • 67