Counting Lines

This is a discussion on Counting Lines within the C++ Programming forums, part of the General Programming Boards category; How do you count lines in a file? And how do you read to the different lines? I know this ...

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Counting Lines

    How do you count lines in a file? And how do you read to the different lines? I know this question has been asked before but I never found a straight answer.

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    hope this helps:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main()
    {
         ifstream read;			// create our stream
         string content;			// create a string to hold the content of the file
    
         read.open("file.ext");	               // open the file (make sure it exists)
    
         while(read >> content)		// create a while loop to read the file
         {
            cout << content;		// print the file's contents
         }
    
         read.close();		               // close the file
         return 0;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions. You can read in every line and keep count. Another solution is to read in raw data and find "\n."

    Kuphryn

  4. #4
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    your code doesn't quite make since, abrege, i think read and content are the same things... but i'm not sure.

  5. #5
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question

    Anyone Else?

  6. #6
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Code:
    ifstream infile;
    
    infile.open("SomeTextfile.txt");
    int lines=0;
    string strLine;
    if(infile.good())
    {   while(!infile.eof() && infile.good())
         {   getline(infile,strLine);
              lines++;
         }
    }
    infile.close();

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    It's like what kuphryn said, loop until you reach '\n' and that's a line, make a counter to keep track of the number of '\n's, and that's it...

    does this answer your question.

  8. #8
    Registered User nevermind's Avatar
    Join Date
    Oct 2002
    Posts
    23

    Using Vectors

    Hi, I think this should help:
    Code:
    //using vectors to open file and count lines
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<vector>
    
    int main()
    {
    
       vector<string> v;            
       string line;
       ifstream in("something");
       int i;
       int count=0;
    
        while(in.getline(in, line))
           v.push_back(line);
        
        for(int i=0; i<v.size(); i++){
            cout << v[i] << endl;
            count += i;
       }
       
        cout << "Total lines: " << count;
    
        return 0;
    }
    I just learnt this stuff from Bruce Eckels Thinking in C++
    I hope it helped.
    "Cut the foreplay and just ask, man."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting groups of lines
    By Ayreon in forum C Programming
    Replies: 26
    Last Post: 03-04-2009, 09:51 AM
  2. Counting the number of lines in a text file - help
    By Erkan in forum C Programming
    Replies: 11
    Last Post: 11-12-2005, 04:12 PM
  3. Beginner problem with counting lines in a file
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-25-2005, 05:47 PM
  4. Counting Lines
    By darfader in forum C Programming
    Replies: 6
    Last Post: 09-12-2003, 06:19 AM
  5. Counting Number of Lines of file
    By dapernia in forum C Programming
    Replies: 1
    Last Post: 09-05-2003, 02:22 PM

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