| | |
miserable fgets and a 2d array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
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?
Where
Prints out the question onto a terminal from a certain processor.

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?
C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
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.
•
•
Join Date: Apr 2006
Posts: 26
Reputation:
Solved Threads: 0
Why do you hate fgets. Its my faviorate function.
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
with
Notes for your code.
1) If fopen fail on the file, you still calling fgets. Error.
2) replace
why add 1 ?
array subscript from 0 to 24 !!
C Syntax (Toggle Plain Text)
char* fgets(char *s, int n, FILE *stream);
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
C Syntax (Toggle Plain Text)
fgets(questions[x], sizeof(questions), fp)
C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
aRandom = (rand()%25)+1;
array subscript from 0 to 24 !!
>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.
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
> 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
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
C Syntax (Toggle Plain Text)
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 */ }
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: iterations in linked queues
- Next Thread: Gauss Elimination problem
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h







