Hello!

Is there any way that I could create a loop that will user the content from a text-file and the number of lines? I want to go through a file and look for something, and then I need to use line by line, not char by char.

Maybe the easyset way would be to read the entire file line by line into an vector, and then use the vector<>::iterator to loop through the lines?

Then the question is:
How do i read the lines of an file into an vector?

>How do i read the lines of an file into an vector?

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

int main()
{
  std::ifstream in ( "myfile" );

  if ( in ) {
    std::vector<std::string> lines;
    std::string line;

    while ( std::getline ( in, line ) )
      lines.push_back ( line );

    // Process the vector here
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.