Thread: read lines

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

    read lines

    Is there a simple code or function I can implement into my code to read the number of lines in a given file?


    thanx


    jujubeats

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use a counter and the getline function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    18
    i was on the correct path using the getline but i havent been able so far let me keep on trying

    thanx

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Don't know of any standard C function that does this, but you could just scan the file and count the number of newline characters occurring.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but you could just scan the file and count the number of newline characters occurring.
    This screams fencepost error. What if the last line is terminated with end-of-file instead than a newline? Your count would be off by one.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    18
    the file only contains binary inputs

    so scanf would be also a good resource?

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    18
    oh no scanf is the same as cin

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >the file only contains binary inputs
    When you want to count the lines in a file, it's usually expected that the file contains text. This is mostly because newlines vary depending on the system, and any attempt to read one in a binary oriented file could easily fail. Maybe you should give us an example of the file, describe what you mean by a "line", and explain what you intend to do with the count. Then we'll be in a better position to offer suggestions beyond the usual suspects.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    18
    well sticking to words instead of binary inputs

    lets say I have

    dogs
    cats
    plane
    fish

    i already opened the file I just need to put a subroutine to count
    the lines in that text file i opened i.e. 4 lines in this previous example
    and then display the number of lines along with its contents on another file
    (which i've done with ofstream but im missing the actual code to read the # of lines succesfully )

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    So is it binary or text? That's kind of an issue in determining a solution. If it's text, you can just loop with getline:
    Code:
    while ( getline ( in, line ) ) {
      // Increment a count, store the line, whatever
    }
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    18
    its just a series of matrices

    instead of cat and dog

    its more like

    000
    010
    100
    111

    but i just need to do it for a file at a time
    such as this example

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >its more like
    Okay, that's text which just happens to represent binary values. So you can approach the problem from that perspective and use getline.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    18
    thanx

    im trying to but i think im using it as to read from the keyboard instead of my file thats why its not working

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    At this point you need to post your code, or we'll be completely unable to help you (or refuse to).
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    18
    I apologize heres what my code looks like

    Code:
    #include "stdafx.h"
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main()
    {
       
          char str[20];
            fstream file_op("c:\\andop.txt",ios::in);
            while(file_op >> str)
            cout << " \n" << str << endl;
    
    	
            file_op.close();
    
    		ofstream file_wr("c:\\outfile.txt", ios::out);
    		  file_wr.close();
    
            return 0;
    
    }
    the andop(and operator) only contains a four line matrix
    Last edited by jujubeats; 11-02-2006 at 05:08 PM.

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, 07: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, 10:30 PM
  4. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM