Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems parsing a file in C
    primarykey
    data
    text
    <p>I'm trying to parse a set of files that all have the same format. Here's an example instance:</p> <pre><code>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 </code></pre> <p>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:</p> <pre><code>fp = fopen(argv[1] , "r"); for (i = 0; i &lt; 3; ++i) { fscanf(fp, "\n"); } fscanf(fp, "%d", &amp;size); for (i = 0; i &lt; 3; ++i) { fscanf(fp, "\n"); } cost = (double**) calloc(size, sizeof(double*)); for(i = 0 ; i &lt; size; ++i){ cost[i] = (double*) calloc(size, sizeof(double)); } for(i = 0 ; i &lt; size; ++i) { for(j = 0 ; j &lt; size; ++j) { fscanf(fp, "%lf", &amp;(cost[i][j])); } cost[i][i] = 0; } fclose(fp); </code></pre> <p>(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!)</p> <p>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?</p> <p>EDIT: I've tried Vicky's suggestion of <code>fscanf(fp, "%s\n", buf);</code> - 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 <code>"%*s"</code> and <code>"%*s\n"</code> didn't write anything at all to buf.</p> <p>EDIT 2: <code>while((c = getchar()) != '\n' &amp;&amp; c != 'EOF') ;</code> just hangs the program. No idea what it's doing.</p> <p>EDIT 3: <code>while((c = getc(fp)) != '\n' &amp;&amp; c != 'EOF') ;</code> is going through the file line by line, but <code>fscanf(fp, "%d", &amp;size);</code> still isn't picking up the number.</p> <p>EDIT 4: Aha! Got it working with</p> <pre><code>char c; for (i = 0; i &lt; 3; ++i) { while((c = getc(fp)) != '\n' &amp;&amp; c != 'EOF') ; } fscanf(fp, "%*s"); fscanf(fp, "%i", &amp;size); for (i = 0; i &lt; 4; ++i) { while((c = getc(fp)) != '\n' &amp;&amp; c != 'EOF') ; } </code></pre> <p>Thanks for the help, all!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload