Note that there are some explanatory texts on larger screens.

plurals
  1. POInfinite-loop when reading from a text file
    primarykey
    data
    text
    <p>I have read several questions on this site and managed to write the code below which reads data from a file (shown before the code, below). The problem is that the code gets in an infinite loop in the <code>do {...} while</code> segment, indicating that it does not "recognise" the newline char. I have no idea what to do. Also when I pass from the command-line the filename to read, it crashes. Could someone please help?</p> <p>Input File Format:</p> <pre><code>1 3 3 3 1 23588769 1 2 1 4 4 9 4788939 3 2 1 2 2 4 5 5 2 3 3 1 4 3 5 17215766 5 2 1 1 4 5 5 4 </code></pre> <p>Code:</p> <pre><code>int main (int argc, char *argv[]) { FILE *fp = NULL; if (argc == 1) { fp = stdin; } else { fp = fopen( argv[1], "r" ) ; } fp = fopen( argv[1], "r" ) ; if (fp == NULL) exit(2); for(i=0; i&lt;42; i++) KEYS_I_HAVE[i]=0; fscanf( fp,"%d",&amp;num_start_keys); fscanf(fp, "%d",&amp;doors); KEYS_FOR_DOORS = (int*)malloc(doors*sizeof(int)); POINTS = (int*)malloc(doors*sizeof(int)); for(i=0; i&lt;doors; i++) { KEYS_FOR_DOORS[i]=0; POINTS[i]=0; } MAP = malloc(doors*sizeof(int)); for(i=0; i&lt;43; i++) MAP[i] = malloc(43*sizeof(int)); for(i=0; i&lt;doors; i++) { for (j=0; j&lt;43; j++) MAP[i][j] = 0; } for(i=0; i&lt;(doors+1); i++) { j=0; if(i==0) { do { fscanf(fp,"%d",&amp;temp); printf("%d",temp); KEYS_I_HAVE[temp]++; a=fgetc(fp); }while(a != '\n'); } else { do { fscanf(fp,"%d",&amp;temp); if (j==0) sum += temp; else { if (j==1) KEYS_FOR_DOORS[i-1] = temp; else { if(j==2) POINTS[i-1]=temp; else MAP[i-1][temp]++; } } a=fgetc(fp); j++; if (feof(fp) ) break; }while( a != '\n' ); } } fclose(fp); </code></pre>
    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