how to read each line in text??

This is a discussion on how to read each line in text?? within the C Programming forums, part of the General Programming Boards category; hello, I want to read a line in a file1.txt and check if the first letter contains 'a' or 's' ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    how to read each line in text??

    hello,

    I want to read a line in a file1.txt and check if the first letter contains 'a' or 's' , then print it in another file file2.txt.


    I am trying it by fread and fwrite.

    I did it for single line in file1.txt.
    ie., I define character string to 100 characters , as char c[100];

    I restrict to read a line of 20 characters only. ie. fread(c,1,20,file2);

    but, I cann't able to read the next line to check whether it stars with 'a' or 's'.

    what can i do to read each line in the file1.txt file.


    thank you.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    The thing with fread is that is reads 20 objets of 1 byte, so therefore you may or may not be reading a line every time (as it could read in the newline character and then it would continue on the next line). What you really want is fgets. That'll do the job for you.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    I prefer to use fgets() for reading text files, but it's your call.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-01-2007, 07:17 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 01:35 PM
  4. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM

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