I'm trying to parse a set of files that all have the same format. Here's an example instance:
NAME: br17
TYPE: ATSP
COMMENT: 17 city problem (Repetto)
DIMENSION: 17
EDGE_WEIGHT_TYPE: EXPLICIT
EDGE_WEIGHT_FORMAT: FULL_MATRIX
EDGE_WEIGHT_SECTION
9999 3 5 48 48 8 8 5 5 3 3 0 3 5 8 8
5
3 9999 3 48 48 8 8 5 5 0 0 3 0 3 8 8
5
5 3 9999 72 72 48 48 24 24 3 3 5 3 0 48 48
24
48 48 74 9999 0 6 6 12 12 48 48 48 48 74 6 6
12
48 48 74 0 9999 6 6 12 12 48 48 48 48 74 6 6
12
8 8 50 6 6 9999 0 8 8 8 8 8 8 50 0 0
8
8 8 50 6 6 0 9999 8 8 8 8 8 8 50 0 0
8
5 5 26 12 12 8 8 9999 0 5 5 5 5 26 8 8
0
5 5 26 12 12 8 8 0 9999 5 5 5 5 26 8 8
0
3 0 3 48 48 8 8 5 5 9999 0 3 0 3 8 8
5
3 0 3 48 48 8 8 5 5 0 9999 3 0 3 8 8
5
0 3 5 48 48 8 8 5 5 3 3 9999 3 5 8 8
5
3 0 3 48 48 8 8 5 5 0 0 3 9999 3 8 8
5
5 3 0 72 72 48 48 24 24 3 3 5 3 9999 48 48
24
8 8 50 6 6 0 0 8 8 8 8 8 8 50 9999 0
8
8 8 50 6 6 0 0 8 8 8 8 8 8 50 0 9999
8
5 5 26 12 12 8 8 0 0 5 5 5 5 26 8 8
9999
EOF
I'm wanting to lift out the dimension of the matrix and the matrix itself, everything else can be discarded. This is the code I'm currently using to try and parse it:
fp = fopen(argv[1] , "r");
for (i = 0; i < 3; ++i)
{
fscanf(fp, "\n");
}
fscanf(fp, "%d", &size);
for (i = 0; i < 3; ++i)
{
fscanf(fp, "\n");
}
cost = (double**) calloc(size, sizeof(double*));
for(i = 0 ; i < size; ++i){
cost[i] = (double*) calloc(size, sizeof(double));
}
for(i = 0 ; i < size; ++i)
{
for(j = 0 ; j < size; ++j)
{
fscanf(fp, "%lf", &(cost[i][j]));
}
cost[i][i] = 0;
}
fclose(fp);
(The file does seem to have newlines when I open it in a text editor - though not in Notepad - I don't know why they've vanished here. NAME, TYPE, COMMENT, DIMENSION, EDGE_WEIGHT_TYPE, EDGE_WEIGHT_FORMAT, and EDGE_WEIGHT_SECTION all appear starting new lines. EDIT: Ah, thanks, Yossarian. I'm a Stack Overflow newbie!)
Anyway, my code isn't working. Specifically, I've noticed through using a debugger that it isn't lifting the dimension of the matrix, which means that the attempt to read the matrix properly is doomed from the start. All variables are declared, that's not the problem. It's just not reading the number after dimension and assigning it to size. What am I doing wrong?
EDIT: I've tried Vicky's suggestion of fscanf(fp, "%s\n", buf);
- which also has the advantage of letting me see where it is in the file by watching the value of buf - and discovered that it's taking one word at a time, not one line. Trouble with that approach is that the COMMENT: line isn't consistent in the number of words it has. Using "%*s"
and "%*s\n"
didn't write anything at all to buf.
EDIT 2: while((c = getchar()) != '\n' && c != 'EOF') ;
just hangs the program. No idea what it's doing.
EDIT 3: while((c = getc(fp)) != '\n' && c != 'EOF') ;
is going through the file line by line, but fscanf(fp, "%d", &size);
still isn't picking up the number.
EDIT 4: Aha! Got it working with
char c;
for (i = 0; i < 3; ++i)
{
while((c = getc(fp)) != '\n' && c != 'EOF') ;
}
fscanf(fp, "%*s");
fscanf(fp, "%i", &size);
for (i = 0; i < 4; ++i)
{
while((c = getc(fp)) != '\n' && c != 'EOF') ;
}
Thanks for the help, all!