miserable fgets and a 2d array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

miserable fgets and a 2d array

 
0
  #1
Apr 24th, 2006
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?

  1. void displayQuestion() {
  2.  
  3. /* fp is a FILE pointer. This pointer will read text from
  4.   the "questions.txt" file */
  5.  
  6. int aRandom, i, x;
  7. char questions[25][300];
  8. FILE *fp = fopen("questions.txt", "rt");
  9.  
  10. if (*fp == NULL) {
  11. printf("Invalid File\n");
  12. }
  13.  
  14. for(x = 0; x < 25; x++){
  15.  
  16.  
  17. while( fgets(questions[x], sizeof(questions), fp) != NULL) {
  18.  
  19. }
  20.  
  21.  
  22. }
  23.  
  24. aRandom = (rand()%25)+1;
  25.  
  26.  
  27. for(i = 0; i < 300; i++)
  28. {
  29. temp2=SCISR1;
  30. temp2=SCIDRL;
  31.  
  32. while((SCISR1 & 0b10000000) != 0b10000000){}
  33.  
  34.  
  35. SCIDRL = question[aRandom][i];
  36. }
  37.  
  38.  
  39.  
  40. //closes the file
  41. fclose(fp);
  42.  
  43.  
  44. getButtonPress();
  45.  
  46. }

Where
  1. for(i = 0; i < 300; i++)
  2. {
  3. temp2=SCISR1;
  4. temp2=SCIDRL;
  5.  
  6. while((SCISR1 & 0b10000000) != 0b10000000){}
  7.  
  8.  
  9. SCIDRL = question[aRandom][i];
  10. }

Prints out the question onto a terminal from a certain processor.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: miserable fgets and a 2d array

 
0
  #2
Apr 24th, 2006
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.


Piworld
[Tis simple as Pie]
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 26
Reputation: dude543 is an unknown quantity at this point 
Solved Threads: 0
dude543 dude543 is offline Offline
Light Poster

Re: miserable fgets and a 2d array

 
0
  #3
Apr 24th, 2006
Why do you hate fgets. Its my faviorate function.


  1. 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
  1. fgets(questions[x], sizeof(questions), fp)
with
  1. 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
  1. aRandom = (rand()%25)+1;
why add 1 ?

array subscript from 0 to 24 !!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: miserable fgets and a 2d array

 
0
  #4
Apr 24th, 2006
gets() is my favourite function. :lol:


Piworld
[Tis simple as Pie]
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,810
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: miserable fgets and a 2d array

 
0
  #5
Apr 24th, 2006
>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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: miserable fgets and a 2d array

 
0
  #6
Apr 25th, 2006
> 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
  1. for ( x = 0 ;
  2. x < 25 && fgets(questions[x], sizeof(questions[x]), fp) != NULL )
  3. x++ ) {
  4. char *p = strchr( questions[x], '\n' );
  5. if ( p ) *p = '\0'; /* blow away a newline - if you want to that is */
  6. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC