943,693 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1155
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
May 2nd, 2008
0

Help reading a text file of any size

Expand Post »
Hi,
I really need some help with this project. I'm currently a beginning C student working on a wheel of fortune program. For this program a random phrase must be chosen from a text file; the same phrase cannot also be used again during the game (there are three rounds). I've started to work on this, but I'm really stuck. I think I need to have a loop with fgets instead but other than that I'm clueless Please help me. Thanks. The test text file had 43 lines that why rand is with 43
  1. int initialize_array(char phrase[], char puzzle[], char clue[]){
  2. FILE* phraseFile; /*input file*/
  3. char temp[100]; /*temporary array*/
  4. int line; /*line from which word and clue are chosen*/
  5. size_t i=0; /*counter for loop*/
  6. int len; /*length of line from file*/
  7.  
  8.  
  9. line=rand()%43; /*randomization of line*/
  10.  
  11. phraseFile = fopen("clues.txt", "r"); /*opens file*/
  12.  
  13. if (!phraseFile) {
  14. fprintf(stderr, "Oops - file not opened !\n"); /*if there is error in opening file program is exited*/
  15. exit(1);
  16. }
  17.  
  18. while(line--){
  19. fgets(temp, 100, phraseFile); /*gets line and stores in temp*/
  20. }
  21.  
  22. len=strlen(temp);
  23. if(temp[len-1]=='\n')
  24. temp[len-1]=0; /*places the null terminating character after word*/
  25.  
  26. /*separates clues from word*/
  27. strcpy(clue,strtok(temp, "%"));
  28. char tmp[WORD_LENGTH];
  29. strcpy(tmp,strtok(NULL,"%"));
  30. strcpy(phrase, tmp + 1);
  31. strcpy(puzzle,phrase);
  32.  
  33.  
  34. /*strcpy(puzzle,phrase);*/
  35.  
  36. while(i<strlen(puzzle)){
  37. if(isalpha(puzzle[i])){
  38. puzzle[i] = '*'; /*hides word*/
  39. }
  40. ++i;
  41. }
  42. fclose(phraseFile); /*closes file*/
  43. return i;
  44. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
helloan is offline Offline
5 posts
since May 2008
May 2nd, 2008
0

Re: Help reading a text file of any size

The hard part is working out how to get a random line from the file when you don't know how many lines there are. One way is to read the file once and count the lines, then use that count to pick a random number. A cooler way is a survivor method. Read lines and use a random test to pick between two lines. The one you picked is the survivor. When you get to the end of the file, the survivor will be randomly picked from all of the lines:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. char *getLine(FILE *fp, char line[], size_t limit);
  7. void getRandomLine(const char *filename, char line[], size_t limit);
  8.  
  9. int main()
  10. {
  11. char line[1024];
  12. int i;
  13.  
  14. srand((unsigned)time(NULL));
  15.  
  16. /* Get 10 random lines from the file */
  17. for (i = 0; i < 10; ++i) {
  18. getRandomLine("test.txt", line, sizeof line);
  19. printf("%s\n", line);
  20. }
  21. }
  22.  
  23. char *getLine(FILE *fp, char line[], size_t limit)
  24. {
  25. char *result = fgets(line, limit, fp);
  26.  
  27. if (result != NULL) {
  28. /* Strip the newline character */
  29. char *nl = strchr(line, '\n');
  30.  
  31. if (nl != NULL)
  32. *nl = '\0';
  33. }
  34.  
  35. return result;
  36. }
  37.  
  38. void getRandomLine(const char *filename, char line[], size_t limit)
  39. {
  40. FILE *fp = fopen(filename, "r");
  41.  
  42. if (fp != NULL) {
  43. /* Get the first line as the first survivor */
  44. if (getLine(fp, line, limit) != NULL) {
  45. char *temp = (char*)malloc(limit);
  46.  
  47. /* Survivor method starting with the first 2 lines */
  48. while (getLine(fp, temp, limit) != NULL) {
  49. if (rand() % 2 == 0)
  50. strcpy(line, temp);
  51. }
  52.  
  53. /* Clean up resources */
  54. free(temp);
  55. fclose(fp);
  56. }
  57. }
  58. }
It gets kind of inefficient if you ask for a lot of random lines though. The best solution is to keep the whole file in memory and just use random indexes if it's small enough.
Last edited by Radical Edward; May 2nd, 2008 at 12:28 pm.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
May 2nd, 2008
0

Re: Help reading a text file of any size

I was thinking of doing it the first way described. Here is what I've done so far, but it isn't working. Someone please help!
  1. int initialize_array(char phrase[], char puzzle[], char clue[]){
  2. FILE* phraseFile; /*input file*/
  3. char temp[100]; /*temporary array*/
  4. char line[100]; /*line from which word and clue are chosen*/
  5. size_t i=0; /*counter for loop*/
  6. int len; /*length of line from file*/
  7. int count;
  8. int x=0;
  9.  
  10.  
  11. phraseFile = fopen("clues.txt", "r"); /*opens file*/
  12.  
  13. if (!phraseFile) {
  14. fprintf(stderr, "Oops - file not opened !\n"); /*if there is error in opening file program is exited*/
  15. exit(1);
  16. }
  17.  
  18. count=0;
  19. while(fgets(temp, 100, phraseFile)!=NULL){
  20. ++count;
  21. }
  22.  
  23. x=rand()%count+1;
  24.  
  25. len=strlen(temp);
  26. if(temp[len-1]=='\n')
  27. temp[len-1]=0; /*places the null terminating character after word*/
  28.  
  29. /*separates clues from word*/
  30. strcpy(clue,strtok(temp, "%"));
  31. char tmp[WORD_LENGTH];
  32. strcpy(tmp,strtok(NULL,"%"));
  33. strcpy(phrase, tmp + 1);
  34. strcpy(puzzle,phrase);
  35.  
  36.  
  37. /*strcpy(puzzle,phrase);*/
  38.  
  39. while(i<strlen(puzzle)){
  40. if(isalpha(puzzle[i])){
  41. puzzle[i] = '*'; /*hides word*/
  42. }
  43. ++i;
  44. }
  45. fclose(phraseFile); /*closes file*/
  46. return i;
  47. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
helloan is offline Offline
5 posts
since May 2008
May 2nd, 2008
0

Re: Help reading a text file of any size

You count the lines, but you don't go back and get the random line. Your function will always pick the last line in the file. This is what you want:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. int main()
  7. {
  8. FILE *fp = fopen("test.txt", "r");
  9. char line[1024];
  10. int count = 0;
  11. int r;
  12.  
  13. srand((unsigned)time(NULL));
  14.  
  15. while (fgets(line, sizeof line, fp) != NULL)
  16. ++count;
  17.  
  18. r = rand() % count;
  19.  
  20. // Start over
  21. rewind(fp);
  22.  
  23. // Only read r lines
  24. while (--r >= 0)
  25. fgets(line, sizeof line, fp);
  26.  
  27. puts(line);
  28.  
  29. return 0;
  30. }
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
May 2nd, 2008
0

Re: Help reading a text file of any size

well, for one thing:
  1. size_t i=0; /*counter for loop*/
  2. int len; /*length of line from file*/
you've got your types switched. not really going to make a difference (size_t is typically #defined as a long int) but still...

if this basic stuff is wrong, i wonder how much more typos and cut-and-paste errors are lurking.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 2nd, 2008
0

Re: Help reading a text file of any size

Is there anyway to do this without using rewind? We haven't learned this yet.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
helloan is offline Offline
5 posts
since May 2008
May 3rd, 2008
0

Re: Help reading a text file of any size

sure. instead of using "rewind", merely close the file, then reopen it exactly as you did the first time.

either way you start at the beginning of the file. rewind is just a nicer way to do it.



PS: dont be afraid to try new functions. especially if they're part of standard ANSI C libraries. rewind is part of the <stdio.h> library.

it's not some sort of secret 33rd level Grand Poobah Royal Order of Ancient Waterbuffalo thing.
Last edited by jephthah; May 3rd, 2008 at 1:12 am.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 3rd, 2008
0

Re: Help reading a text file of any size

Click to Expand / Collapse  Quote originally posted by helloan ...
Is there anyway to do this without using rewind? We haven't learned this yet.
Just close and open the file again.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
May 3rd, 2008
0

Re: Help reading a text file of any size

its like deja vu or something.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 3rd, 2008
0

Re: Help reading a text file of any size

Well, use any function that does the job.

besides, rewind() is nothing but

fseek (/* file pointer*/fp ,0L, SEEK_SET ) ;
Reputation Points: 94
Solved Threads: 33
Posting Whiz
Prabakar is offline Offline
342 posts
since May 2008

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: Conversion to inches
Next Thread in C Forum Timeline: how to reverse a string





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


Follow us on Twitter


© 2011 DaniWeb® LLC