944,092 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3302
  • C RSS
Apr 24th, 2006
0

miserable fgets and a 2d array

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Apr 24th, 2006
0

Re: miserable fgets and a 2d array

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]
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 24th, 2006
0

Re: miserable fgets and a 2d array

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 !!
Reputation Points: 12
Solved Threads: 0
Light Poster
dude543 is offline Offline
26 posts
since Apr 2006
Apr 24th, 2006
0

Re: miserable fgets and a 2d array

gets() is my favourite function. :lol:


Piworld
[Tis simple as Pie]
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 24th, 2006
0

Re: miserable fgets and a 2d array

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 25th, 2006
0

Re: miserable fgets and a 2d array

> 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. }
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: iterations in linked queues
Next Thread in C Forum Timeline: Gauss Elimination problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC