Thread: reading words line by line from a file

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    reading words line by line from a file

    I am using fgets to read words line by line, however using fgets I always get the extra '\n' in each line.. how do I eliminate it?

    say I have this list:

    Apple
    Bubble
    Man
    Zebra

    then I use fgets, I get Apple\n, how do I get Apple only?
    Last edited by -EquinoX-; 05-03-2008 at 11:58 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not check return value of fgets before using it
    you do not check return value of malloc before using it
    you do not free allocated memory
    you casting malloc: FAQ: Casting malloc?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    sorry I just changed my question

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I am using fgets to read words line by line, however using fgets I always get the extra '\n' in each line.. how do I eliminate it?
    You really have to study FAQ before asking futher questions...
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading text file by line number
    By whiskedaway in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2009, 10:09 AM
  2. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  3. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  4. Program crashes on reading a line from a file
    By bluescreen in forum C Programming
    Replies: 5
    Last Post: 10-19-2006, 05:23 AM
  5. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM