Thread: append to file with write()

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    20

    append to file with write()

    Hi!

    I have an array with two digits:

    Code:
    int digits[2];
    dprintf(digits, "%d", number);
    Now I want to append those digits to my textfile:

    Code:
    write(writeToFile, digits, sizeof(digits));
    But how come the result looks like this in the textfile, what was appended was this:
    ^A^D^@^@^B^@^@^@
    Would someone know the reason for this?

    thank you!

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    write() writes bytes to the file, but you probably want to write text.

    Look at fprintf().

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    dprintf() probably adds a terminating null. If you have only two characters, you will corrupt memory. This can cause all sorts of strange things to happen.
    But I suspect that there's also a problem with the way you opened the file. Since you didn't post that section of your code, it's impossible to diagnose it.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    20
    ok so I dig in to fprintf.

    I make a FILE like this:
    Code:
    FILE *fp = fopen(argv[1], "a+"); //read and append
    And append the file with this:
    Code:
    fprintf(fp, "\n\n%d", number);
    the variable number is an int and contains 3.

    This time nothing hapens, should this not be all needed to append this textfile?
    Last edited by Gatsu; 03-17-2013 at 11:48 AM.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Please post a complete (non)working program not just random lines.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    20
    ok I pasted all my random lines togeather here:



    bash$ ./a.out file.txt
    Last edited by Gatsu; 03-17-2013 at 01:01 PM.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    20
    NEVERMIND!!! I found out that its because I did not use fclose(fp)!! victory! thank you all very much for yout time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How i an append a file and write at its start
    By fredsilvester93 in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2012, 11:22 PM
  2. Append one file to another.
    By guillermoh in forum C Programming
    Replies: 6
    Last Post: 02-04-2008, 12:04 PM
  3. file append
    By rahulsk1947 in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 01:01 AM
  4. Append to a file
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 07-12-2005, 02:12 AM
  5. append file?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2001, 08:37 AM