Thread: reading file line by line

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    3

    reading file line by line

    Dear All
    I am beginer to C . I want to read file line by line as string.
    I used fgets but how to locate end of line ? Is there any other way ?
    Help will be highly appriciated.
    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read this FAQ entry. Use something like strchr to locate your newline character. If it doesn't show up, you haven't reached the end of the line, and instead have reached the end of your buffer.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    you could use the fscanf function and just add a newline to that or you could also use fgetc and read while its !='\n'

    or if you want to stick with fgets try what quzah said.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    Also bear in mind the fact that you'll get both a carriage return AND a linefeed character if the file is open in binary mode ("rb" specified in fopen), and just a linefeed if opened in text mode ("r" specified in fopen).

    I would write a simple function to get characters using a while loop, and terminate at eof or end of line. The prototype for such a function could look like this:

    Code:
    int readfile(FILE *handle, char *outbuf)
    where handle is the fopened handle to read from, and outbuf is a pointer to memory allocated before the function call to take the output string. Don't forget to zero-terminate the resultant string.

    Return length of the line read in or -1 for end of file.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    I have a question. I've been wondering how does other systems process end-of-line? Do they all put "\r\n" at the end?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It shouldn't matter to your code if you use fopen() in text mode.
    Whatever the OS uses, you only see a \n at the end of the line when reading
    When writing, you just append a \n, and it gets translated to whatever the OS uses.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Angoid
    Also bear in mind the fact that you'll get both a carriage return AND a linefeed character if the file is open in binary mode ("rb" specified in fopen), and just a linefeed if opened in text mode ("r" specified in fopen).
    As Salem suggested, this is not entirely accurate. In *nix, there is no "\r" written to text files. In MacOS, IIRC, there is no "\n". They only write one character, not the pair of them. Windows uses both. DOS uses both. And all of the rest of the OSs use whatever they like. The point is, you cannot use the above blanket statement to cover all of the OSs and hope to be correct.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    As Salem suggested, this is not entirely accurate.
    Point taken .... I was thinking in Windows terms when I posted that rather than *nix as I don't tend to do much C hacking on *nix systems.
    My apologies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 12:34 AM
  3. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM