   Reading different lines in text file
  Hello everyone. So iv been looking for a way to read different lines from a text file at a given time.So this is what i have right now. Some reason its reading the last line an displaying it.I wanna read the fist one in the txt.Im thinking i have my counter messed up somewhere.#include <iostream>#include <fstream>using namespace std;int main(){ifstream myfile("area.txt");string line,buffer[1];const size_t size = sizeof buffer / sizeof *buffer;size_t i = 0;while (getline(myfile,line)){  buffer = line;  if (++i >= size)  {   i=0;  }}for (size_t j = 0; j < size; ++j){  cout << buffer << "\n";  if (++i >= size)  {   i = 0;  }}system("pause");}
  I am kind of new to this but perhaps my experiences can help you...getline() When used for the first time will get the first line in the file, so if you only need to read the first line, you only really need to use getline() once.If you want to read a line other than the first, use getline() LINE many times. For example, say you want to read line 5.... use getline() five times.That's kind of a cheap way of doing it I believe, so I looked for alternatives and found seekg(). seekg() can set the istream pointer for the next line. So you can set the position of the istream "next line" then use getline() once as long as you know the starting position number for the line.Hope this helps;  The problem you're having here is that you've created an array of size 1 (ie. just 1 string) to hold each line of the file. When you're reading the file you reset the counter (variable i) whenever it exceeds the size (variable size). Single-stepping through the code with your debugger should give a clear view of what's going on. I would recommend replacing that array with a vector<> of strings.This way you don't need to worry about overflowing a buffer because the vector worries about that for you ;Hello everyone. So iv been looking for a way to read different lines from a text file at a given time.So this is what i have right now. Some reason its reading the last line an displaying it.I wanna read the fist one in the txt.Im thinking i have my counter messed up somewhere.<-- code snippet removed -->Don't use arrays, vectors are much more powerful, much easier to use, and make your task much easier. Also, don't use sizeof, it doesn't do what you think it does. Here's how to do what you want:#include <iostream>#include <fstream>#include <vector>using namespace std;int main(){    ifstream myfile("area.txt");    string line;    //Your new buffer is an array - you'll thank yourself for this later.    vector<string> buffer;    //Continue grabbing lines from the file, as long as there are lines to get.    //The 'eof()' function means 'end of file'.    //So we are saying, "while myfile is not at the end of the file, get the next line, and add it to the buffer"    while (myfile.eof() != true)    {   //Get the next line from the file.   getline(myfile,line);   //Add the line to the top of the vector.   buffer.push_back(line);    }    //Loop through the vector, printing out the contents.    for(unsigned int j = 0; j < buffer.size(); j++) //I do j++, not ++j, otherwise you'll skip the first line.    {   cout << buffer[j] << "\n";    }    system("pause");}[font="arial, verdana, tahoma, sans-serif"][/font][font="arial, verdana, tahoma, sans-serif"]Don't hesitate with any questions if you don't understand something.[/font]   ;  I don't quite get what you're trying to do there.  Are you trying to select which line in a file you want read?As your code stands now you could do the exact same thing with the following:std::ifstream myfile("area.txt");std::string buffer;while (myfile.good()) {std::getline(myfile, buffer);}std::cout << buffer << std::endl;All your code is doing is reading through the entire file, setting buffer to every line along the way until you reach the end of the file.  So your buffer is just going to be the last line in your file.What is your code supposed to be doing?   ;  Wow thank you guys. ill look into useing a vector. But anyways its for a text based game. My code has a bunch of couts an i wanna clean it up a little bit an store all of that in a text file.What i want to do is make a file that holds the story for that area.like say if the player just started up the game an you start in a house. Well i want to hold the the story in a file an call the first paragraph.Then after the player picks his choice the next description will show.I hope that explains it a little better.   ;[quote name='DSchade' timestamp='1299983850' post='4785078']Hello everyone. So iv been looking for a way to read different lines from a text file at a given time.So this is what i have right now. Some reason its reading the last line an displaying it.I wanna read the fist one in the txt.Im thinking i have my counter messed up somewhere.<-- code snippet removed -->Don't use arrays, vectors are much more powerful, much easier to use, and make your task much easier. Also, don't use sizeof, it doesn't do what you think it does. Here's how to do what you want:#include <iostream>#include <fstream>#include <vector>using namespace std;int main(){    ifstream myfile("area.txt");    string line;    //Your new buffer is an array - you'll thank yourself for this later.    vector<string> buffer;    //Continue grabbing lines from the file, as long as there are lines to get.    //The 'eof()' function means 'end of file'.    //So we are saying, "while myfile is not at the end of the file, get the next line, and add it to the buffer"    while (myfile.eof() != true)    {   //Get the next line from the file.   getline(myfile,line);   //Add the line to the top of the vector.   buffer.push_back(line);    }    //Loop through the vector, printing out the contents.    for(unsigned int j = 0; j < buffer.size(); j++) //I do j++, not ++j, otherwise you'll skip the first line.    {   cout << buffer[j] << "\n";    }    system("pause");}[font="arial, verdana, tahoma, sans-serif"][/font][font="arial, verdana, tahoma, sans-serif"]Don't hesitate with any questions if you don't understand something.[/font][/quote]I understand everything but the vectors. Thats because i havent done much research on them.The code works. But now i wanna beable to stop after line 1 so thats the only word i see. So next time i call the text file i can read the second line.   ;I understand everything but the vectors. Thats because i havent done much research on them.Vectors are very very similar to arrays, but have additional features as well. For example, vectors can be resized, added to, removed from, and emptied.Vectors also keep record of the size of the vector for you.The code works. But now i wanna beable to stop after line 1 so thats the only word i see. So next time i call the text file i can read the second line.[/quote]You can't really do that, even if you used an array. A better idea, is to read the entire file, store it in the vector, and then just read the vector when you want, and you can read any line in the vector.#include <iostream>#include <fstream>#include <vector>using namespace std;//We'll create our own ReadLine() function, for convenience.string ReadLine(vector<string> &buffer, int line){    //Check to see if the buffer even has that many lines in it (because if we    //call 'buffer[N]' where 'N' is larger than the buffer, then the program crashes,    //so we check first, to keep the program from crashing)    if(line >= buffer.size())    {   return "<that line doesn't exist>";    }    //Read the line from the buffer.    return buffer[line];}int main(){    ifstream myfile("area.txt");    vector<string> buffer;    //Continue grabbing lines from the file, as long as there are lines to get.    while (myfile.eof() != true)    {string line;   //Get the next line from the file.   getline(myfile,line);   //Add the line to the top of the vector.   buffer.push_back(line);    }    //Read through the lines in any order we want. (Don't forget that the first line is line '0' in C++)    cout << "First line: " << ReadLine(buffer, 0) << "\n";    //We can read the lines in any order we want:    cout << "Twenty-third line: " << ReadLine(buffer, 22) << "\n";    cout << "Fourth line: " << ReadLine(buffer, 3) << "\n";    cout << "Fifth line: " << ReadLine(buffer, 4) << "\n";    cout << "Fourth line again: " << ReadLine(buffer, 3) << "\n";    cout << "Second line: " << ReadLine(buffer, 1) << "\n";    system("pause");} [color="#1C2837"][font="arial, verdana, tahoma, sans-serif"][/font];  Ah cool thanks servant. I read through the code an did some research on vectors, an now i have a little more knowledge on them.The code you supplied works great.  Now can i make something like ReadPara an add more [font="Courier New"]parameters in it, so i cancall more lines so i can display a paragraph instead of individual lines.Sorry for not using correct words or proper typing. I havent had to type or write in 4 years.Im a welder an im about to start going to school for science an information systems.[/font]   