Append to File

Discussion in 'C Programming' started by Jonathan Shan, Jul 27, 2006.

  1. Hello everybody,

    For some strange reason my output to file keeps writing to the top
    instead of bottom.

    Here is the code that exhibits the problem:

    void printfile(int i)
    {
    FILE* write;
    write = fopen("write.txt", "a+");
    fprintf(write, "%i \n", i);
    }
    int main()
    {

    int i;
    for (i = 0; i < 10 ; i++)
    {
    printfile(i);
    }
    return 0;
    }

    The output is:
    9
    8
    7
    ....
    instead of the "expected"
    0
    1
    2
    ....

    I already realize one solution is to reverse the for loop..

    Jonathan Shan
    Jonathan Shan, Jul 27, 2006
    #1
    1. Advertising

  2. On Thu, 27 Jul 2006, Jonathan Shan wrote:

    > Hello everybody,
    >
    > For some strange reason my output to file keeps writing to the top
    > instead of bottom.
    >
    > Here is the code that exhibits the problem:
    >
    > void printfile(int i)
    > {
    > FILE* write;
    > write = fopen("write.txt", "a+");
    > fprintf(write, "%i \n", i);
    > }
    > int main()
    > {
    >
    > int i;
    > for (i = 0; i < 10 ; i++)
    > {
    > printfile(i);
    > }
    > return 0;
    > }
    >
    > The output is:
    > 9
    > 8
    > 7
    > ...
    > instead of the "expected"
    > 0
    > 1
    > 2
    > ...
    >
    > I already realize one solution is to reverse the for loop..
    >
    > Jonathan Shan


    You should close the file before reopening it.

    Tak-Shing
    Tak-Shing Chan, Jul 27, 2006
    #2
    1. Advertising

  3. Jonathan Shan wrote:
    >
    > Hello everybody,
    >
    > For some strange reason my output to file keeps writing to the top
    > instead of bottom.
    >
    > Here is the code that exhibits the problem:
    >
    > void printfile(int i)
    > {
    > FILE* write;
    > write = fopen("write.txt", "a+");
    > fprintf(write, "%i \n", i);
    > }


    You create a new FILE* each time you call printfile(), and you
    never close them.

    > int main()
    > {
    >
    > int i;
    > for (i = 0; i < 10 ; i++)
    > {
    > printfile(i);
    > }
    > return 0;
    > }
    >
    > The output is:
    > 9
    > 8
    > 7
    > ...
    > instead of the "expected"
    > 0
    > 1
    > 2
    > ...
    >
    > I already realize one solution is to reverse the for loop..


    What you have done is open the "write.txt" file 10 times, and
    written the numbers to each separately buffered stream. When
    main() returns, the C runtime library closes the files that you
    have left open, in some order known only to the compiler writers.
    >From the behavior, it appears that it closes them in the reverse

    order you opened them, causing that stream's output to be appended
    to the file.

    The solution is to either fclose() the file in printfile(), or
    only open the file once.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Jul 27, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. uy_do
    Replies:
    1
    Views:
    670
    Harish Madhavan
    Dec 4, 2003
  2. Dilantha Seneviratne

    How to append to a text file

    Dilantha Seneviratne, Jan 5, 2004, in forum: Java
    Replies:
    10
    Views:
    9,866
    Andrew Thompson
    Jan 9, 2004
  3. DX
    Replies:
    3
    Views:
    5,575
  4. HYRY
    Replies:
    10
    Views:
    600
    Bruno Desthuilliers
    Sep 26, 2007
  5. PerlFAQ Server
    Replies:
    0
    Views:
    385
    PerlFAQ Server
    Feb 24, 2011
Loading...

Share This Page