468,513 Members | 1,837 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Append to File

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

Jul 27 '06 #1
2 17395
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
Jul 27 '06 #2
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:Th*************@gmail.com>
Jul 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by David Beck | last post: by
5 posts views Thread by Mark | last post: by
1 post views Thread by BW | last post: by
29 posts views Thread by jaysherby | last post: by
9 posts views Thread by =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post: by
18 posts views Thread by dainova | last post: by
17 posts views Thread by gayathrib1 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.