Reading Lines from Text File

Reply

Join Date: Oct 2008
Posts: 8
Reputation: Madd0g17 is an unknown quantity at this point 
Solved Threads: 0
Madd0g17 Madd0g17 is offline Offline
Newbie Poster

Reading Lines from Text File

 
0
  #1
Oct 20th, 2008
I have searched many a site to find an answer to this question, so here goes:

I'm doing a hangman program that needs to pull different quotes from a text file named quotes.txt. Each quote is on a different line. I have the entire hangman program written (I can post later, if need be), but I need to write a function to get the quote, calculate the quote length, AND convert the entire quote to uppercase(I thnk I'm fine with that part though)

My variables are as follows:
(X is the length of the quote which will need to be calculated)
char realquote[x] contains the quote as it is written in the text file.
char quote[x] contains the quote written in all capitals

The function begins as follows:

  1. void GetQuote()
  2. {
  3. //This is where I need code to pull the quote
  4. }


I will have approximately 10 quotes stored in the text file (One per line, as mentioned before). I would like to generate a random number between 1 and 10 and pull the quote from that line into the "realquote" array. I then need to determine the actual string length (I'm assuming I will use strlen or something like that?).

Any and all help with this would be greatly appreciated. Like I said, I can post up the entire Hangman program so you can see how it fits in, but I believe I've provided all relevant information to this specific part!

Thanks!
Madd0g

P.S. Right now I am using <stdio.h> <string.h> and <ctype.h> I can probably use some others (I'm using whatever comes with Visual Studio 2008) if I have to! Thanks again!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Reading Lines from Text File

 
0
  #2
Oct 21st, 2008
Take it one step at a time.
Start figuring how to generate the random number from one to ten.
After that lean how to open a file for reading.
Follow with reading a line from file and storing it in a buffer.
Once you got that down, you can start bringing all that knowledge to work together.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: Madd0g17 is an unknown quantity at this point 
Solved Threads: 0
Madd0g17 Madd0g17 is offline Offline
Newbie Poster

Re: Reading Lines from Text File

 
0
  #3
Oct 21st, 2008
I know the random number should be something like:
line = rand() % 10 + 1;
the file open part should be:
FILE * pFile; 
pFile = fopen("quotes.txt","r");
(at least that's what the examples I've seen show)

then later on i'll need to close the file:
fclose(pFile);
The part I'm really struggling with is reading a line from the file and storing it into the arrays!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Reading Lines from Text File

 
0
  #4
Oct 21st, 2008
>The part I'm really struggling with is reading a line from the file and storing it into the arrays!
Alright! I think I have just what you need. Read on.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: Madd0g17 is an unknown quantity at this point 
Solved Threads: 0
Madd0g17 Madd0g17 is offline Offline
Newbie Poster

Re: Reading Lines from Text File

 
0
  #5
Oct 21st, 2008
Ok...I now have this much:
  1. void GetQuote()
  2. {
  3. int whichquote;
  4. whichquote = rand() % 10 + 1;
  5. FILE*pFile;
  6. pFile = fopen("quotes.txt","r");
  7.  
  8. if(pFile!=NULL) //IF FILE OPENS
  9. {
  10. while(fgets(quote, sizeof quote, pFile)!=NULL)
  11. {
  12. fputs(quote,stdout);
  13. }
  14. fclose(pFile);
  15. }
  16. else
  17. {
  18. perror("quotes.txt");
  19. }
  20. }

Now do I have to create a 2-D array and save all the quotes into there respective arrays, or can I fetch a different one from the text file later?

Also, is there a way to scan to see how many lines are in the text file so it can generate a random number based off of that?
Last edited by Madd0g17; Oct 21st, 2008 at 12:52 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Reading Lines from Text File

 
0
  #6
Oct 21st, 2008
I know this is not what you want to hear, but here it is nonetheless.
Go back to the example and study it a little more.
Create a text file and write on it a hundred times:
"Next time I will not copy and paste C code I don't understand."
Save the file with your choice of identifier.
Write a program that will open that file and display each line to standard output.
If you get stuck use your favorite search engine to find out more. Reading from file it is not a "strange" concept, therefore you are going to be lucking finding more than you need, all over the Internet or this site.
Once you have that part done. Report your progress over here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: Madd0g17 is an unknown quantity at this point 
Solved Threads: 0
Madd0g17 Madd0g17 is offline Offline
Newbie Poster

Re: Reading Lines from Text File

 
0
  #7
Oct 21st, 2008
Ok...I know this is a little bit of a change from what I state previously...After some thought, I've decided that what I really need is TWO functions. One that runs during the "setup" that puts all the different quotes into arrays, then a second function that I can use to return a quote whenever I need one.



The second function would be called in the form GetQuote(Number); where Number would be generated by rand() % TotalNumberOfQuotes + 1 . This function also needs to convert whatever quote it pulls to an identical quote but in all caps! (For example, if it pulls quote[]= "This is just a test." then it also needs to generate realquote[]="THIS IS JUST A TEST." ) The reason for this is because when the hangman program is checking to see if the letter the user guesses is in the phrase or if it's already been guessed, it compares everything versus the capital letters (otherwise 'A' and 'a' would be considered two different letters).

This being said, the first function would therefore need to assign each quote to an array as well as generate the total number of quotes. I imagine it would be a two-dimensional array such as array[a][b] where "b" is the line number, "a" is the actual quote.


BTW, here is my entire hangman program so far:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <windows.h>
  5.  
  6. char answer;
  7. char quote[]; //Length determined by strlen?
  8. char realquote[]; //Length determined by strlen?
  9. int guessedquote[]; //Length determined by strlen?
  10. char letters[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  11. int usedletters[26];
  12. char guess;
  13. int guesses;
  14. int done;
  15. int total;
  16. int i,j;
  17. int length;
  18. int TotalQuotes;
  19.  
  20.  
  21. char GetQuote()
  22. {
  23. return(quote,realquote);
  24. }
  25.  
  26. int RetrieveQuotes()
  27. {
  28.  
  29. return TotalQuotes;
  30. }
  31.  
  32.  
  33. char response() //Gets a single character response
  34. {
  35. answer=getchar(); //Gets first character typed
  36. while(getchar()!='\n'){} //Waits for you to type the rest of your gibberish and press enter
  37. return answer; //Returns 'answer' as a character that can be compared too
  38. }
  39.  
  40. void Reset() //DONE
  41. {
  42. done = 0;
  43. guesses=7;
  44. for(i=0;i<26;i++)
  45. {
  46. usedletters[i]=0;
  47. }
  48. for(i=0;i<length;i++)
  49. {
  50. if(isalpha(quote[i]))
  51. {
  52. guessedquote[i]=0;
  53. }
  54. else
  55. {
  56. guessedquote[i]=1;
  57. }
  58. }
  59. }
  60.  
  61. void PrintGuesses() //DONE
  62. {
  63. printf("You have %i guesses remaining!\n",guesses);
  64. }
  65.  
  66. void PrintPhrase() //DONE
  67. {
  68. for(i=0;i<(length-1);i++)
  69. {
  70. if(guessedquote[i]==0)
  71. {
  72. printf("*");
  73. }
  74. else
  75. {
  76. printf("%c",realquote[i]);
  77. }
  78. }
  79. printf("\n");
  80. }
  81.  
  82. void PrintLettersUsed() //DONE
  83. {
  84. printf("Letters Used: ");
  85. for(i=0;i<26;i++)
  86. {
  87. if(usedletters[i]==1)
  88. {
  89. printf("%c ",letters[i]);
  90. }
  91. }
  92. printf("\n");
  93. }
  94. void PrintHangman()
  95. {
  96. if(guesses==7)
  97. {
  98. printf(" _____ \n");
  99. printf(" / |\n");
  100. printf(" | \n");
  101. printf(" | \n");
  102. printf(" | \n");
  103. printf(" | \n");
  104. printf(" | \n");
  105. printf("_|_ \n");
  106. }
  107. if(guesses==6)
  108. {
  109. printf(" _____ \n");
  110. printf(" / | \n");
  111. printf(" | O \n");
  112. printf(" | \n");
  113. printf(" | \n");
  114. printf(" | \n");
  115. printf(" | \n");
  116. printf("_|_ \n");
  117. }
  118. if(guesses==5)
  119. {
  120. printf(" _____ \n");
  121. printf(" / | \n");
  122. printf(" | O \n");
  123. printf(" | | \n");
  124. printf(" | \n");
  125. printf(" | \n");
  126. printf(" | \n");
  127. printf("_|_ \n");
  128. }
  129. if(guesses==4)
  130. {
  131. printf(" _____ \n");
  132. printf(" / | \n");
  133. printf(" | O \n");
  134. printf(" | \\| \n");
  135. printf(" | \n");
  136. printf(" | \n");
  137. printf(" | \n");
  138. printf("_|_ \n");
  139. }
  140. if(guesses==3)
  141. {
  142. printf(" _____ \n");
  143. printf(" / | \n");
  144. printf(" | O \n");
  145. printf(" | \\|/\n");
  146. printf(" | \n");
  147. printf(" | \n");
  148. printf(" | \n");
  149. printf("_|_ \n");
  150. }
  151. if(guesses==2)
  152. {
  153. printf(" _____ \n");
  154. printf(" / | \n");
  155. printf(" | O \n");
  156. printf(" | \\|/\n");
  157. printf(" | | \n");
  158. printf(" | \n");
  159. printf(" | \n");
  160. printf("_|_ \n");
  161. }
  162. if(guesses==1)
  163. {
  164. printf(" _____ \n");
  165. printf(" / | \n");
  166. printf(" | O \n");
  167. printf(" | \\|/\n");
  168. printf(" | | \n");
  169. printf(" | / \n");
  170. printf(" | \n");
  171. printf("_|_ \n");
  172. }
  173. if(guesses==0)
  174. {
  175. printf(" _____ \n");
  176. printf(" / | \n");
  177. printf(" | O \n");
  178. printf(" | | \n");
  179. printf(" | \\|/\n");
  180. printf(" | | \n");
  181. printf(" | / \\\n");
  182. printf(" | \n");
  183. printf("_|_ \n");
  184. }
  185. }
  186.  
  187.  
  188. void GetLetter() //DONE
  189. {
  190. int used = 0;
  191. printf("Your Guess: ");
  192. response();
  193. if(isalpha(answer))
  194. {
  195. guess = toupper(answer);
  196. for(i=0;i<length;i++)
  197. {
  198. if(quote[i]==guess)
  199. {
  200. guessedquote[i]=1;
  201. used=1;
  202. }
  203. }
  204. for(i=0;i<26;i++)
  205. {
  206. if((letters[i]==guess)&&(usedletters[i]==1))
  207. {
  208. printf("\nYou have already guessed that letter!");
  209. Sleep(1000);
  210. used=1;
  211. }
  212. if(letters[i]==guess)
  213. {
  214. usedletters[i]=1;
  215. }
  216. }
  217. }
  218. else
  219. {
  220. printf("That is not a letter!\n");
  221. Sleep(1000);
  222. used=1;
  223. }
  224. if(used==0)
  225. {
  226. guesses--;
  227. }
  228. total = 0;
  229. for(i=0;i<length;i++)
  230. {
  231. if(guessedquote[i]==1)
  232. {
  233. total++;
  234. }
  235. }
  236. if(total==length)
  237. {
  238. done = 1;
  239. }
  240. }
  241.  
  242.  
  243. void hangman()
  244. {
  245. Reset();
  246. system("cls");
  247. PrintHangman();
  248. PrintGuesses();
  249. PrintPhrase();
  250. PrintLettersUsed();
  251. do
  252. {
  253. GetLetter();
  254. system("cls");
  255. PrintHangman();
  256. PrintGuesses();
  257. PrintPhrase();
  258. PrintLettersUsed();
  259. }
  260. while((guesses>0)&&(done==0));
  261. if(done==1)
  262. {
  263. printf("\nCongratulations! You have guessed the phrase!\n");
  264. printf("The phrase was: %s\n\n",realquote);
  265. }
  266. else
  267. {
  268. printf("\nSorry, you have run out of guesses! You have been HUNG!\n");
  269. printf("The phrase was: %s\n\n",realquote);
  270. }
  271. return;
  272. }
  273.  
  274. int main(void) //MAIN PROGRAM LOOP
  275. {
  276. printf("*NOTICE: ONLY THE FIRST CHARACTER YOU TYPE (FOR ANY RESPONSE) WILL BE USED!*\n\n");
  277. printf("Please press enter twice to continue...");
  278. while(response()!='\n') {}
  279. RetrieveQuotes();
  280. int Num;
  281. do
  282. {
  283. Num = rand() % TotalQuotes + 1;
  284. GetQuote(Num);
  285. hangman(); //Do Hangman Program
  286. printf("Would you like to play again? (Y/N) "); //Ask if you would like to repeat
  287. response(); //Get user response
  288. system("cls"); //Print user's answer so I know what it is!
  289. }
  290. while((answer=='Y')||(answer=='y'));
  291. system("cls");
  292. printf("Thank You for playing! Goodbye!\n\n"); //Dismiss user!
  293. return 0;
  294. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Reading Lines from Text File

 
0
  #8
Oct 21st, 2008
Did you learn how to read from file?

For simplicity purposes:
  1. char quotes[3][12] = { "firstquote", "secondquote", "thirdquote" };
  2. int random_number = 1;
  3. printf("%s\n", quotes[random_number]);
Which quote would be displayed by printf? Maybe the answer would help you when the time comes to get the wanted quote.
Last edited by Aia; Oct 21st, 2008 at 4:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: Madd0g17 is an unknown quantity at this point 
Solved Threads: 0
Madd0g17 Madd0g17 is offline Offline
Newbie Poster

Re: Reading Lines from Text File

 
0
  #9
Oct 21st, 2008
That example should print "firstquote"
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: Madd0g17 is an unknown quantity at this point 
Solved Threads: 0
Madd0g17 Madd0g17 is offline Offline
Newbie Poster

Re: Reading Lines from Text File

 
0
  #10
Oct 21st, 2008
I'm going to post the code that was referenced in the thread you sent me to, adding in my comments for what each line is doing...Maybe you can help fill in the ??'s then!

  1. int main ( void )
  2. {
  3. static const char filename[] = "data.txt"; //Maddog - Sets the filename
  4. FILE *file = fopen ( filename, "r" ); //Maddog - Opens the file
  5. int i, j;
  6. char arra[128][128]; //Maddog - Initializes the array[][] variable and sets the size to 128x128
  7. char line[128]; /* or other suitable maximum line size */ //Maddog - Initializes a variable that will contain a single line
  8.  
  9. for(i=0; i<128; i++) //Maddog - ???? It seems that he is setting everything in the array to '\0' (not sure why though)
  10. {
  11. for(j=0; j<128; j++)
  12. {
  13. arra[i][j] = '\0';
  14. }
  15. }
  16.  
  17. for(i=0; i<128; i++) //Maddog - ???? Same as above, setting everything in the line array to '\0'
  18. {
  19. line[i] = '\0';
  20. }
  21.  
  22.  
  23. if ( file != NULL ) //Maddog - If files opens do...
  24. {
  25. i=0;
  26. //Maddog - ???? While not NULL (end of file??) get a line and the size of the line from the file
  27. while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
  28. {
  29. strcpy(arra[i], line); //Maddog - Copy string from the arra to line
  30. printf("array ----> %s ", &arra[i]); //Maddog - print the string you just pulled
  31. i++;
  32. }
  33. fclose ( file ); //Maddog - Close file
  34. }
  35. else //Maddog - If file doesn't open...
  36. {
  37. perror ( filename ); /* why didn't the file open? */ //Maddog - Spit out error message to tell you that file didn't open
  38. }
  39. return 0;
  40. }
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