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

Re: Help reading a text file of any size

 
0
  #11
May 3rd, 2008
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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
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: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Help reading a text file of any size

 
0
  #12
May 4th, 2008
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.
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



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC