how to check for end of line in a text file

This is a discussion on how to check for end of line in a text file within the C++ Programming forums, part of the General Programming Boards category; i needed to write a prog to display which line a character is found in, in a text file so ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    how to check for end of line in a text file

    i needed to write a prog to display which line a character is found in, in a text file

    so basically my problem is checking for the new line or end of line

    i mean
    if(ch=='\n')
    line++;

    doesnt work

    someone tells me cr(ascii val = 13) and lf(ascii val = 10)
    are the chars to check for
    what would be the syntax if i need to check
    one char from file as equal to 2 ascii values
    can anyone give me the syntax of that one if statement to check for end of line

    be a help

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Check for the new linefeed character (ASCII 10).

    In Windows, you find that text file line ends are marked by a carriage return & linefeed pair. While in Unix/Linux they are marked by just a linefeed.

    You may want to read up to linefeed, & then check whether the character before it is a carriage return & perform the necessary action.

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    i needed to write a prog to display which line a character is found in, in a text file
    you can place eof() in your code and check it that way. I believe its ctrl Z to execute it...at least if your using MV C++.
    I hope this helps you!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Why if(ch=='\n') doesn't work??
    isn't it the ASCII code for the newline and line feed?

  5. #5
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    Why if(ch=='\n') doesn't work??
    Yes it does. I think I was confused on what he was asking for. I apologize and Thanks for the clarification, ammar.
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why not use getline. You'll have to set the max line length, so it won't work too well if the input data is longer than you expect.

    i mean
    if(ch=='\n')
    line++;

    doesnt work
    It works for me... maybe you should show some code
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    Smile

    thanks guys for ure help

    i figured out what the problem was
    i was using
    fin>>ch;
    but that is error prone and was screwing up in this case
    aaaahhhh i know

    so i used fin.get(ch)
    worked fine

    if(ch=='\n')
    is the right condition for end of line
    carriage return(cr) and line feed(lf)
    are new line characters when u open the file in binary mode

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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