Thread: reading lines sequentially from a file

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    11

    reading lines sequentially from a file

    Hi,
    I have a file "a.txt". I want to read the contents of the file line by line and output them. a.txt has the following input:

    a.txt:
    Text
    file
    1
    2
    3

    Can anyone help me, how can I use fscanf and fprintf to read and output these files? Thanks

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Smells like homework. Did you try it yourself?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use a loop:

    1. Read a line (attempt to read a line).
    2. If successfull, output it. If not, exit loop.
    3. Rinse, repeat.
    "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

  4. #4
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    In Windows, there are actually two chars at the end of a line: \xD and \xA. Note, they exist only in text files. But in *nix, only one: \xA. So you can open a.txt with binary mode, and read a line by comparing current char with \xD or \xA (under *nix).
    Last edited by TalosChen; 05-09-2006 at 10:59 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Can anyone help me, how can I use fscanf and fprintf to read and output these files?
    fgets() and fputs() would be better
    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.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    11
    I don't know how can someone "smell" a homework. If you do not know the answer please say so instead of showing off to other people how good you are. I have done programming in C++ and Visual in span across advanced conpets like hashing, stacks, queue, collection, btree, graph problems etc. Its just that I am trying to convert this program from Fortran to C and I am not familiar with some of the formats of C.
    For example Fortran reads a line sequentially using the following statement:
    read(10, 200) project_name
    What I wanted to know if fscanf it works the same way in C? i.e. if fscanf takes a line at a time like the fortram read statement.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It can, provided you know how long the string is. This will read LEN characters and a turminating NUL character into string. And then read a newline and do nothing with it.
    Code:
     fscanf(fp, "%*s\n", LEN, string);
    However this is not the best practice. If you get input shorter than LEN characters it won't work as good because the \n gets put in the string buffer, and then fscanf reads to the end of the file or until the buffer is filled.

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. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Reading file into edit - losing new lines
    By eam in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2003, 01:07 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM