Reading next line of a file

how do you read the next line of a file and print it out? this is my code

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
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
     if (line = something)
      {
      // read next line and print it... but how?
      }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

Your if statement is incorrect. You probably meant == and not just =; also the getline() has already read the line into line; if you want to read the next line, just use getline() again. To print it out, just use std::cout.
Also, don't use eof() in your loop condition. Use good() instead (which can be done implicitly as follows):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  string line;
  ifstream myfile( "example.txt" );
  if (myfile)  // same as: if (myfile.good())
    {
    while (getline( myfile, line ))  // same as: while (getline( myfile, line ).good())
      {
      if (line == something)
        {
        ...
        }
      }
    myfile.close();
    }
  else cout << "fooey\n";

  return 0;
  }

Hope this helps.
cool thanks, one more question.. so this is what my text file looks like

------
apple, banana, peach
1
computer, mouse, keyboard
2
sun, earth, moon
3
-------


how can i make it so if it finds any one of those 3 words it prints the next line? right now i just have one word and it works but i need different keywords. PS and how do i make it not cap sensitive?


like if it finds banana it types 1 or MOuSe it types 2
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <algorithm>
#include <cctype>
#include <functional>
#include <string>

std::string uppercase( const std::string& s )
  {
  std::string result( s.length(), ' ' );
  std::transform(
    s.begin(),
    s.end(),
    result.begin(),
    std::ptr_fun <int, int> ( std::toupper )
    );
  return result;
  }

Convert your string to uppercase (or lowercase) and compare it that way:
1
2
3
4
if (uppercase( "Hello" ) == "HELLO")
  cout << "yeah!\n";
else
  cout << "fooey!\n";

Use find() to search for substrings.
http://www.cplusplus.com/reference/string/string/find/

Hope this helps.
hi Duoas, i understand your code i just dont know where to put it inside mine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
  string line;
  string line2;
  ifstream myfile ("itembuilds.txt");
  if (myfile)
      {
    while (getline( myfile, line ))  
        {
			if (uppercase( line ) == uppercase( Payload ))
          {
			 (getline (myfile, line2 ));
		     SendChat( player, Payload + ": " + line2 );
          }
        }
    myfile.close();
        
	   }
It seems to me you are more interested in matching only some part of the line, for example (pseudocode):

if ("banana" in "yes we have no bananas")

First convert both to uppercase (or lowercase -- whichever is more appropriate) and then find() it:

if (uppercase( "yes we have no bananas" ).find( uppercase( "Banana" ) ) != string::npos)

I don't really know exactly what you are trying to do, but your example looks fine...

BTW, in your text editor, tell it to turn off tabs... it messes with the layout when you cut and paste here.

Good luck!
when i do this i declare a counter in my if statement then loop the counter in my while condition. Then i say in my final if statment if condition is true i grab that counter and then getline() and display its current line
ok quick question

so say my text file is this:

1, 2, 3, 4, 5
hello
6, 7
hi
8, 9, 10
hola

now with my code the user enters in say 3. i need it to print out hello
or if they enter in 10 i need it to print hola

now i cant use pseudocode because the .txt file will be different everytime. so i guess what im asking for is a way to search the text file for a word. and if that word is anywhere in that line then it displays the next line.

i hope i made it more clear
Well, since the stuff always comes in pairs, just read the file two lines at a time.

Search the first line to see if you can find() the thing the user typed. If it is found, print the second line.

Otherwise, read the next two lines and repeat.

Hope this helps.
ok so this is what i have and the problem i have now is that its only showing the first line whatever you enter in:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string line;
  string line2;
  size_t found;
  bool herofound;
  ifstream myfile ("itembuilds.txt");
  if (myfile)
      {
    while (getline( myfile, line ) && herofound == false)  
    {
		line.find( Payload );
        if ( found!=string::npos && herofound == false )
        {
	    (getline (myfile, line2 ));
		SendChat( player, line + ": " + line2 );
	    herofound = true;
    }
        }
    myfile.close();
        
	  }

Last edited on
any ideas?
hmmm... well maybe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
	while(getline(file,line))
	{
	count++;
		
	                if(search == line)
		{
		int index = count+1;
			if(count = index)
			{
			getline(file,line);
			cout<<"next line is .. "<<line<<endl<<endl;
				
			}
			
		}

	}


this should work, let me know how it works out;

search = the string that the user entered;
index = is just a variable that equals the counter + 1 so it grabs the next line;
Last edited on
Topic archived. No new replies allowed.