read lines

This is a discussion on read lines within the C++ Programming forums, part of the General Programming Boards category; this is the part i was trying to implement unsuccesfully: Code: string t; int lineCount=0; while(getline(file_op, t, '\n')) ++lineCount; cout ...

  1. #16
    Registered User
    Join Date
    Oct 2006
    Posts
    18

    Unhappy

    this is the part i was trying to implement unsuccesfully:

    Code:
    string t;
    int lineCount=0;
    
    while(getline(file_op, t, '\n'))
    ++lineCount;
    
    cout << "The number of lines in the file is " <<
    lineCount << endl;

  2. #17
    Registered User
    Join Date
    Oct 2006
    Posts
    18

    Talking

    i got it ...thanx =)

  3. #18
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45
    Here's what I whipped up:

    Code:
    //#include "stdafx.h"
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    void countLines( istream& in, int& lines )
    {
      char val = '\0';
      lines = 0;
    
      while ( in.get( val ) ) {
        if ( val == '\n' || val == '\r' )
          lines++;
      }
    }
    
    int main( int argc, char* argv[] )
    {
      fstream file_op( "andop.txt", ios::in );
    
      int lines;
      countLines( file_op, lines );
    
      cout << "Lines: " << lines << endl;
      return 0;
    }
    Oh.. were you trying to write to outfile.txt your results?
    I see you got it.. post it!

  4. #19
    Registered User
    Join Date
    Oct 2006
    Posts
    18

    Talking

    oh well i got the part for reading the number of lines... I just modified the small code I typed...however i couldnt display it properly on the output file...but I just thought it was enough bugging you guys...jeje

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count how many lines, allocate vector, and read again
    By patiobarbecue in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2009, 06:18 PM
  2. read Causes Freezing?
    By Masna in forum C Programming
    Replies: 5
    Last Post: 07-18-2008, 04:31 PM
  3. read multiple lines from a file
    By YankeePride13 in forum C Programming
    Replies: 2
    Last Post: 11-10-2005, 09:30 PM
  4. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21