Alright... first off I hate fgets with a passion :)

Anyway, I am wanting to read a file into a 2 dimensional array and then randomly select one of those lines to output...

What am I missing here?

void displayQuestion() {

/* fp is a FILE pointer.  This pointer will read text from 
   the "questions.txt" file */

     int aRandom, i, x;	
     char questions[25][300];	
     FILE *fp = fopen("questions.txt", "rt");

     if (*fp == NULL) {
	printf("Invalid File\n");
     }
	    
     for(x = 0; x < 25; x++){
	
	
     	while( fgets(questions[x], sizeof(questions), fp) != NULL) {
    
     	}
     

     }
	
     aRandom = (rand()%25)+1;

     
    for(i = 0; i < 300; i++)
      {
      temp2=SCISR1;
      temp2=SCIDRL;
     
      while((SCISR1 & 0b10000000) != 0b10000000){}
     
     
      SCIDRL = question[aRandom][i];
      }
          	


     //closes the file
     fclose(fp);


     getButtonPress();

}

Where

for(i = 0; i < 300; i++)
      {
      temp2=SCISR1;
      temp2=SCIDRL;
     
      while((SCISR1 & 0b10000000) != 0b10000000){}
     
     
      SCIDRL = question[aRandom][i];
      }

Prints out the question onto a terminal from a certain processor.

Member Avatar for iamthwee

If you don't like fgets, why don't u read the whole file in as single chars... using the '\n' to distinguish newlines.

a Ha ha.

Why do you hate fgets. Its my faviorate function.

char* fgets(char *s, int n, FILE *stream);

fgets reads at most the next n-1 characters into the array s, stopping
if a newline is encounterd; the newline is included in the array, which
is terminated by '\0'. fgets returns s, or NULL if end of file or error
occurs.

replace

fgets(questions[x], sizeof(questions), fp)

with

fgets(questions[x], sizeof(questions[0]), fp)

Notes for your code.
1) If fopen fail on the file, you still calling fgets. Error.
2) replace

aRandom = (rand()%25)+1;

why add 1 ?

array subscript from 0 to 24 !!

Member Avatar for iamthwee

gets() is my favourite function. :lol:

>Alright... first off I hate fgets with a passion
How can you hate something you don't completely understand? Don't confuse ignorance of fgets (or C in this case) with any legitimate problems that it has.

Just because you don't know how to use something doesn't mean there's anything wrong with it.

> if (*fp == NULL)
You could start here - never mind the rest of it.

Say
if (fp == NULL)

Man, didn't your compiler complain about that line?


> while( fgets(questions[x], sizeof(questions), fp) != NULL)
If you lie about the buffer size, then it's no better than gets()

Besides, if you want to read up to 25 lines, its

for ( x = 0 ;
      x < 25 && fgets(questions[x], sizeof(questions[x]), fp) != NULL )
      x++ ) {
  char *p = strchr( questions[x], '\n' );
  if ( p ) *p = '\0';  /* blow away a newline - if you want to that is */
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.