Reading a file with variable line lengths c++ -
i trying make program reads file line line. lines has variable lengths.
here flow of program:
- first, prompt user enter number of line want view
- basing input user, output line.
i thinking 2 ways implement this:
- i use getline() plus counter, once line, read it
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
Post a Comment