You are here:

C++/String class pointer


What i'm trying to do is find a way to use the getline() function to read from a
file in an open stream and then count the number of characters in that line.  I
have that part but the problem i'm having is getting the getline() to read from
the NEXT line in the file that is open. I was thinking that if i saved the number
of characters in the first string that i could move some pointer to that point in
the file and then begin reading again from that point.  I was looking and i
briefly saw something about seekp() and getp() functions in the fstream but
i'm not sure if that is what i'm looking for.  

The overall goal is to make a program to count the number of lines in the
given file.  Also if there is a more effecient way of doing this i would also
appreciate any ideas.  It was merely the first way i thought of doing it and i
am trying to improve my coding abilities.  

From what you state in your question you appear to be doing the correct thing. std::istream::getline will read up to and including the newline character but not place it in the character buffer you pass to getline. The exception is if the length of the buffer you supply as the second parameter to getline is less than the length of the line - getline will only read up to 1 less than this value (the last character position is used for the C-string style '\0' string termination character). The other reason you may get problems is if the stream state is not good - i.e. has some formatting failure, a bad fatal error or is at the end of the file. Your code should check the state of the stream at appropriate points and take necessary action.

Here is a sample program I wrote using std::ifstream::getline:

#include <fstream>
#include <iostream>
#include <cstring>

int main()
       std::ifstream in("test.txt");

       int const BufferSize( 1024 );
       char lineBuffer[BufferSize];
       int lineNumber(0);

       while ( in.good() )
         in.getline( lineBuffer, BufferSize );

         if ( ! )
         int lineLength( strlen( lineBuffer ) );

         std::cout << "Line " << lineNumber
         << " is " << lineLength
         << " characters long and reads '"
         << lineBuffer << "'\n";


You will note that I check that the stream is good to continue reading lines as the while loop continuation condition and test _after_ reading a line that the stream is not in the failed state. This latter test is not quite the same a being in the good state as it allows the stream to be in the end of file (eof) state. That is the stream is not failed or bad. However being good implies the stream is not in any of the end of file, failed or bad states.

When run using a test.txt file containing the following three lines:


The program produced the following results:

Line 1 is 13 characters long and reads 'FILE TEST.TXT'
Line 2 is 6 characters long and reads 'LINE 2'
Line 3 is 6 characters long and reads 'LINE 3'

As to a more efficient way to count the lines in a file - I have no simple answer. You have to read through all the contents of a file to find each end of line sequence. As lines vary in length there is no other way. A possibly more efficient way would be to read the file in largish chunks and scan each chunk to count any/all newline sequences found within it.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.