Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Append to File

Reply
Thread Tools

Append to File

 
 
Jonathan Shan
Guest
Posts: n/a
 
      07-27-2006
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

 
Reply With Quote
 
 
 
 
Tak-Shing Chan
Guest
Posts: n/a
 
      07-27-2006
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
 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      07-27-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);
> }


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: <(E-Mail Removed)>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.2 How do I change, delete, or insert a line in a file, or append to the beginning of a file? PerlFAQ Server Perl Misc 0 02-24-2011 11:00 PM
Append a file to another file John Wu Ruby 1 05-28-2010 02:40 PM
the address of list.append and list.append.__doc__ HYRY Python 10 09-26-2007 09:41 AM
How to append to a text file Dilantha Seneviratne Java 10 01-09-2004 12:44 PM
JavaService append mode instead of truncating log file uy_do Java 1 12-04-2003 02:42 AM



Advertisments