File Handling in C

Discussion in 'C' started by Poonamol, Mar 31, 2010.

  1. Poonamol

    Poonamol New Member

    Joined:
    Mar 31, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    I want to read a file and from some location i need to copy the contents and write into another file.
    E.g.
    Oldfile.txt contents,
    ABC987;26/09/2006
    ABC988;27/09/2006
    ABC989;28/09/2006
    ABC990;29/09/2006
    ABC991;30/09/2006

    Now while reading a file i need to get some location of line say ABC990;..... and from this line i want to copy contents into another file (till EOF) say Newfile.txt.

    Anyone help me out ASAP. I am a new to file handling in C.
    Thanks in advance.
     
  2. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    fgets() will read a line from a file up to and including the linefeed:

    Code:
    
    FILE *in,*out;
    char buf[100];  /* adjust for longest expected line */
    
    in=fopen("test.txt","r") /* "rt" on Dos/Win */
    out=fopen("output.txt","w");
    
    while(fgets(buf,100,in)){
      /* do something with the line in buf */
      write(buf,100,out);
    }
    fclose(in);
    fclose(out);
    
     
  3. Poonamol

    Poonamol New Member

    Joined:
    Mar 31, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for your reply,
    Instead of write statment, if i do
    fputs(buf, out); => it also writes all data from input file to output file till EOF.

    How can I get the line location?
    For this there is one file is created called (STOP.txt), if this file exists, then get the line which is just read and copy all contents from that line till EOF into output file.

    Please Help me out.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice