Help reading a text file of any size

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

Join Date: May 2008
Posts: 5
Reputation: helloan is an unknown quantity at this point 
Solved Threads: 0
helloan helloan is offline Offline
Newbie Poster

Help reading a text file of any size

 
0
  #1
May 2nd, 2008
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. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: Help reading a text file of any size

 
0
  #2
May 2nd, 2008
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.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 5
Reputation: helloan is an unknown quantity at this point 
Solved Threads: 0
helloan helloan is offline Offline
Newbie Poster

Re: Help reading a text file of any size

 
0
  #3
May 2nd, 2008
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. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: Help reading a text file of any size

 
0
  #4
May 2nd, 2008
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. }
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,607
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Help reading a text file of any size

 
0
  #5
May 2nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 5
Reputation: helloan is an unknown quantity at this point 
Solved Threads: 0
helloan helloan is offline Offline
Newbie Poster

Re: Help reading a text file of any size

 
0
  #6
May 2nd, 2008
Is there anyway to do this without using rewind? We haven't learned this yet.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,607
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Help reading a text file of any size

 
0
  #7
May 3rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Help reading a text file of any size

 
0
  #8
May 3rd, 2008
Originally Posted by helloan View Post
Is there anyway to do this without using rewind? We haven't learned this yet.
Just close and open the file again.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,607
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Help reading a text file of any size

 
0
  #9
May 3rd, 2008
its like deja vu or something.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Help reading a text file of any size

 
0
  #10
May 3rd, 2008
Well, use any function that does the job.

besides, rewind() is nothing but

fseek (/* file pointer*/fp ,0L, SEEK_SET ) ;
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC