reading words line by line from a file

This is a discussion on reading words line by line from a file within the C Programming forums, part of the General Programming Boards category; I am using fgets to read words line by line, however using fgets I always get the extra '\n' in ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

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