| | |
Reading Lines from Text File
![]() |
•
•
Join Date: Oct 2008
Posts: 8
Reputation:
Solved Threads: 0
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:
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!
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:
C Syntax (Toggle Plain Text)
void GetQuote() { //This is where I need code to pull the quote }
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!
•
•
Join Date: Oct 2008
Posts: 8
Reputation:
Solved Threads: 0
I know the random number should be something like:
the file open part should be:
(at least that's what the examples I've seen show)
then later on i'll need to close the file:
The part I'm really struggling with is reading a line from the file and storing it into the arrays!
line = rand() % 10 + 1; the file open part should be:
FILE * pFile;
pFile = fopen("quotes.txt","r");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!
>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.
Alright! I think I have just what you need. Read on.
•
•
Join Date: Oct 2008
Posts: 8
Reputation:
Solved Threads: 0
Ok...I now have this much:
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?
c Syntax (Toggle Plain Text)
void GetQuote() { int whichquote; whichquote = rand() % 10 + 1; FILE*pFile; pFile = fopen("quotes.txt","r"); if(pFile!=NULL) //IF FILE OPENS { while(fgets(quote, sizeof quote, pFile)!=NULL) { fputs(quote,stdout); } fclose(pFile); } else { perror("quotes.txt"); } }
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.
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.
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.
•
•
Join Date: Oct 2008
Posts: 8
Reputation:
Solved Threads: 0
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
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
BTW, here is my entire hangman program so far:
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:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <ctype.h> #include <windows.h> char answer; char quote[]; //Length determined by strlen? char realquote[]; //Length determined by strlen? int guessedquote[]; //Length determined by strlen? 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'}; int usedletters[26]; char guess; int guesses; int done; int total; int i,j; int length; int TotalQuotes; char GetQuote() { return(quote,realquote); } int RetrieveQuotes() { return TotalQuotes; } char response() //Gets a single character response { answer=getchar(); //Gets first character typed while(getchar()!='\n'){} //Waits for you to type the rest of your gibberish and press enter return answer; //Returns 'answer' as a character that can be compared too } void Reset() //DONE { done = 0; guesses=7; for(i=0;i<26;i++) { usedletters[i]=0; } for(i=0;i<length;i++) { if(isalpha(quote[i])) { guessedquote[i]=0; } else { guessedquote[i]=1; } } } void PrintGuesses() //DONE { printf("You have %i guesses remaining!\n",guesses); } void PrintPhrase() //DONE { for(i=0;i<(length-1);i++) { if(guessedquote[i]==0) { printf("*"); } else { printf("%c",realquote[i]); } } printf("\n"); } void PrintLettersUsed() //DONE { printf("Letters Used: "); for(i=0;i<26;i++) { if(usedletters[i]==1) { printf("%c ",letters[i]); } } printf("\n"); } void PrintHangman() { if(guesses==7) { printf(" _____ \n"); printf(" / |\n"); printf(" | \n"); printf(" | \n"); printf(" | \n"); printf(" | \n"); printf(" | \n"); printf("_|_ \n"); } if(guesses==6) { printf(" _____ \n"); printf(" / | \n"); printf(" | O \n"); printf(" | \n"); printf(" | \n"); printf(" | \n"); printf(" | \n"); printf("_|_ \n"); } if(guesses==5) { printf(" _____ \n"); printf(" / | \n"); printf(" | O \n"); printf(" | | \n"); printf(" | \n"); printf(" | \n"); printf(" | \n"); printf("_|_ \n"); } if(guesses==4) { printf(" _____ \n"); printf(" / | \n"); printf(" | O \n"); printf(" | \\| \n"); printf(" | \n"); printf(" | \n"); printf(" | \n"); printf("_|_ \n"); } if(guesses==3) { printf(" _____ \n"); printf(" / | \n"); printf(" | O \n"); printf(" | \\|/\n"); printf(" | \n"); printf(" | \n"); printf(" | \n"); printf("_|_ \n"); } if(guesses==2) { printf(" _____ \n"); printf(" / | \n"); printf(" | O \n"); printf(" | \\|/\n"); printf(" | | \n"); printf(" | \n"); printf(" | \n"); printf("_|_ \n"); } if(guesses==1) { printf(" _____ \n"); printf(" / | \n"); printf(" | O \n"); printf(" | \\|/\n"); printf(" | | \n"); printf(" | / \n"); printf(" | \n"); printf("_|_ \n"); } if(guesses==0) { printf(" _____ \n"); printf(" / | \n"); printf(" | O \n"); printf(" | | \n"); printf(" | \\|/\n"); printf(" | | \n"); printf(" | / \\\n"); printf(" | \n"); printf("_|_ \n"); } } void GetLetter() //DONE { int used = 0; printf("Your Guess: "); response(); if(isalpha(answer)) { guess = toupper(answer); for(i=0;i<length;i++) { if(quote[i]==guess) { guessedquote[i]=1; used=1; } } for(i=0;i<26;i++) { if((letters[i]==guess)&&(usedletters[i]==1)) { printf("\nYou have already guessed that letter!"); Sleep(1000); used=1; } if(letters[i]==guess) { usedletters[i]=1; } } } else { printf("That is not a letter!\n"); Sleep(1000); used=1; } if(used==0) { guesses--; } total = 0; for(i=0;i<length;i++) { if(guessedquote[i]==1) { total++; } } if(total==length) { done = 1; } } void hangman() { Reset(); system("cls"); PrintHangman(); PrintGuesses(); PrintPhrase(); PrintLettersUsed(); do { GetLetter(); system("cls"); PrintHangman(); PrintGuesses(); PrintPhrase(); PrintLettersUsed(); } while((guesses>0)&&(done==0)); if(done==1) { printf("\nCongratulations! You have guessed the phrase!\n"); printf("The phrase was: %s\n\n",realquote); } else { printf("\nSorry, you have run out of guesses! You have been HUNG!\n"); printf("The phrase was: %s\n\n",realquote); } return; } int main(void) //MAIN PROGRAM LOOP { printf("*NOTICE: ONLY THE FIRST CHARACTER YOU TYPE (FOR ANY RESPONSE) WILL BE USED!*\n\n"); printf("Please press enter twice to continue..."); while(response()!='\n') {} RetrieveQuotes(); int Num; do { Num = rand() % TotalQuotes + 1; GetQuote(Num); hangman(); //Do Hangman Program printf("Would you like to play again? (Y/N) "); //Ask if you would like to repeat response(); //Get user response system("cls"); //Print user's answer so I know what it is! } while((answer=='Y')||(answer=='y')); system("cls"); printf("Thank You for playing! Goodbye!\n\n"); //Dismiss user! return 0; }
Did you learn how to read from file?
For simplicity purposes:
Which quote would be displayed by printf? Maybe the answer would help you when the time comes to get the wanted quote.
For simplicity purposes:
c Syntax (Toggle Plain Text)
char quotes[3][12] = { "firstquote", "secondquote", "thirdquote" }; int random_number = 1; printf("%s\n", quotes[random_number]);
Last edited by Aia; Oct 21st, 2008 at 4:29 pm.
•
•
Join Date: Oct 2008
Posts: 8
Reputation:
Solved Threads: 0
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!
C Syntax (Toggle Plain Text)
int main ( void ) { static const char filename[] = "data.txt"; //Maddog - Sets the filename FILE *file = fopen ( filename, "r" ); //Maddog - Opens the file int i, j; char arra[128][128]; //Maddog - Initializes the array[][] variable and sets the size to 128x128 char line[128]; /* or other suitable maximum line size */ //Maddog - Initializes a variable that will contain a single line for(i=0; i<128; i++) //Maddog - ???? It seems that he is setting everything in the array to '\0' (not sure why though) { for(j=0; j<128; j++) { arra[i][j] = '\0'; } } for(i=0; i<128; i++) //Maddog - ???? Same as above, setting everything in the line array to '\0' { line[i] = '\0'; } if ( file != NULL ) //Maddog - If files opens do... { i=0; //Maddog - ???? While not NULL (end of file??) get a line and the size of the line from the file while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */ { strcpy(arra[i], line); //Maddog - Copy string from the arra to line printf("array ----> %s ", &arra[i]); //Maddog - print the string you just pulled i++; } fclose ( file ); //Maddog - Close file } else //Maddog - If file doesn't open... { perror ( filename ); /* why didn't the file open? */ //Maddog - Spit out error message to tell you that file didn't open } return 0; }
![]() |
Similar Threads
- Newbie: problems deleting lines from text file (large) (Python)
- Reading data from text (Again!) (C++)
- reading lines from text file and formating them (Shell Scripting)
- Reading through lines of text file sequencially (Shell Scripting)
- Replace text in a file (Shell Scripting)
- new to c++, reading and storing from a file (C++)
- Echoing TABs? (Shell Scripting)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C Forum
- Previous Thread: itoa function (or similar) in Linux
- Next Thread: how to Detect a position of the word and more
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






