Reading one line from ".txt"

Hi everyone,

First of all i want to point out that i'm a newbie in C++. I just started learning this year, i'm currently studying Software Engineering in university.

My question is how can i read one line from a ".txt" file?
To be more clear, i'm trying to write a hangman game. Therefore i'm trying to get words from a "wordlist.txt" file i created. I know a little bit about ifstream, ofstream and fstream. So my function i created to get a random line from this txt (not-completed yet since i don't know how to get that randomized line, that's what i need help with);

1
2
3
4
5
6
7
8
9
10
11
void grw()
{
	int line;
	srand (time(NULL));
	line = (rand() % 7972) + 1;

	ifstream wordlist;
	wordlist.open("wordlist.txt");
	

}


I created a variable called 'line' for the line number and randomized it (there are 7972 words in ".txt" file). So what i want to do now is to get the word on that line. I need someone to explain me how i can do this? I've read many post about this over internet, most of them are hard to understand for me. So some good explanation step by step would be really appreciated.

Sorry for my English, thanks in advance!
Getline().

http://www.cplusplus.com/reference/string/string/getline/

Edit: Here is a quick example:
1
2
3
4
5
6
7
8
9
10
11
12
#include <fstream>
#include <iostream>

int main()
{
    std::string s; //string
    std::fstream f; //file stream
    f.open("wordlist.txt"); //open your word list
    std::getline(f, s); //get line from f (your word list) and put it in s (the string)
    std::cout << s << "\n"; //output string
    return 0;
}


You could get to a specific line by just looping.
1
2
3
//start loop
std::getline(f, s); //get line from f (your word list) and put it in s (the string)
//end loop 


Last edited on
like mats said, use getline()
See the example about halfway down on this page:
http://www.cplusplus.com/doc/tutorial/files/

1
2
3
4
5
6
7
8
9
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }


You want to add an int called something like lineNumber, initiliaze it to zero and increment inside the while loop and also test that this line number equals the input line number. when it does break out of the loop.
Due to difficulties in moving to a specific spot in a file (you can only tell it how many characters from the start to go), you will need to load the whole file into the memory for the program. Here is how I would do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Create a variable to get rid of "magic numbers"
const size_t LinesInFile = 7972;

// Seed the rng at some point
srand(time(NULL));

// function for getting a word, returning a string
std::string grw() {
    // Stores the data, retains state between calls
    static std::string[LinesInFile] data;
    // Variable testing if the file has been loaded in yet
    static bool isLoaded = false;

    // If the data hasn't been loaded yet
    if (!isLoaded) {
        // Open the file
        std::ifstream wordlist ("wordlist.txt");

        // For the number of words in the file:
        for (size_t i = 0; i < LinesInFile; ++i)
            std::getline(wordlist, data[i]); // Store the word in data

        // Finished with the file, close it
        wordlist.close();
    }

    // return a random word from the data
    return data[rand() % LinesInFile];
}


There are probably better ways of doing this. Try fiddling around with this code, and if there are bits you don't understand, don't hesitate to ask!
Many thanks to all of you, i finally solved it with your helps.
Really appreciated!
Topic archived. No new replies allowed.