Reading a file with variable line lengths c++ -


i trying make program reads file line line. lines has variable lengths.

here flow of program:

  1. first, prompt user enter number of line want view
  2. basing input user, output line.

i thinking 2 ways implement this:

  1. i use getline() plus counter, once line, read it
  2. i use seekg jump position right away , read using getline.(not sure since seekg seeks position);

    i believe seekg ideal use, since faster reading line line (if possible want use seekg) . however, lines have different lengths , im not sure if possible seek lines easily.

    was wondering of give me suggestions.

thanks

use line function , reads complete line (till new line char '\n')

#include< fstream> #include< string> using namespace std;  int main (void) {     string line;     ifstream infile("infile.txt");     while (getline(infile,line))     {         //process each line data here     } } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -