943,725 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1155
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
May 3rd, 2008
0

Re: Help reading a text file of any size

Hi did what was suggested with the fopen/fclose. Could someone now help to me prevent the same line in a text file from being used twice? Thanks
  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. int i=0; /*counter for loop*/
  6. int len; /*length of line from file*/
  7. int count=0;
  8.  
  9. phraseFile = fopen("clues.txt", "r"); /*opens file*/
  10.  
  11. if (!phraseFile) {
  12. fprintf(stderr, "Oops - file not opened !\n"); /*if there is error in opening file program is exited*/
  13. exit(1);
  14. }
  15.  
  16. while(fgets(temp,100,phraseFile)!=0){
  17. ++count;
  18. }
  19.  
  20. fclose(phraseFile);
  21. fopen("clues.txt", "r");
  22. line= rand()%count;
  23.  
  24. while(line--){
  25. fgets(temp, 100, phraseFile); /*gets line and stores in temp*/
  26. }
  27.  
  28. len=strlen(temp);
  29. if(temp[len-1]=='\n')
  30. temp[len-1]=0; /*places the null terminating character after word*/
  31.  
  32. /*separates clues from word*/
  33. strcpy(clue,strtok(temp, "%"));
  34. /*
  35. strcpy(phrase,strtok(NULL,"%"));*/
  36. char tmp[WORD_LENGTH];
  37. strcpy(tmp,strtok(NULL,"%"));
  38. strcpy(phrase, tmp + 1);
  39. strcpy(puzzle,phrase);
  40.  
  41.  
  42. /*strcpy(puzzle,phrase);*/
  43.  
  44. while(i<strlen(puzzle)){
  45. if(isalpha(puzzle[i])){
  46. puzzle[i] = '*'; /*hides word*/
  47. }
  48. ++i;
  49. }
  50. fclose(phraseFile); /*closes file*/
  51. return i;
  52. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
helloan is offline Offline
5 posts
since May 2008
May 4th, 2008
0

Re: Help reading a text file of any size

in "main", declare an integer array of size equal to or greater than the number of possible lines, initializing all elements to zero on startup.


each time you go to use a line, check to see that the array element at that line index is still zero. if not, choose a different line.


once you use a line from the file, set the value of the array element at that line's index number to one.
Last edited by jephthah; May 4th, 2008 at 2:27 am.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 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