954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

miserable fgets and a 2d array

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.

jhdobbins
Junior Poster
105 posts since Apr 2005
Reputation Points: 10
Solved Threads: 3
 

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.

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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 !!

dude543
Light Poster
26 posts since Apr 2006
Reputation Points: 12
Solved Threads: 0
 

gets() is my favourite function. :lol:

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

> 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 */
}
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You