Thread: how to add new data into file .txt

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    17

    how to add new data into file .txt

    Hi all, i don't know how to add new data to file .txt. example i have file with name test.txt it contain
    house home
    dress clothes
    car motor
    ... ...
    i want to add you us to the last file. so the file would be like this
    house home
    dress clothes
    car motor
    you us
    what should i do?
    sorry for bad english

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use fopen() with 'a' (append) for the second arg.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want to simply add some more data to the end of a text file, open the file in "a" mode (append mode), and then print to the file, just like normal. Append mode moves the file pointer to the very end of the file being opened.

    Don't forget to close the file, later!
    Last edited by Adak; 12-31-2011 at 10:44 AM.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There isn't an a+w mode, just r, w, a, r+ (read+write), w+ (write+read), a+ (write-to-end+read).

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    17
    it is my script
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        FILE *PF;
        char C;
        if((PF=fopen("E:/Kuliah/Progtur/tugas besar/kosa kata/coba.txt","a"))== NULL)
        {
            printf("file tidak dapat dibuka");
        }
    
        while((C=getche())!='\r')
        {
            fputc(C,PF);
        }
        fclose(PF);
    }
    but it is write the data right next to the last data. it does not write below the last data. what should i do??
    Last edited by riris; 12-31-2011 at 11:08 AM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you need to put this on a new line then you may need to add a new line ('\n') before you write the new information.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-25-2011, 05:54 PM
  2. Replies: 2
    Last Post: 05-06-2011, 07:43 AM
  3. Adding data to existing data in an input file?
    By matthayzon89 in forum C Programming
    Replies: 4
    Last Post: 11-20-2010, 11:23 PM
  4. Replies: 21
    Last Post: 11-03-2007, 02:56 PM
  5. Replies: 2
    Last Post: 06-16-2005, 10:03 AM