Thread: read lines

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 )

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