473,395 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 17640
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
4
by: David Beck | last post by:
I donwnload some files for processing every day that have unwanted characters in them. In VB6 I use the InputB to read in the text and the StrConv. vLinesFromFile =...
5
by: Mark | last post by:
Can I bind datagrid to an xml string (not to file)? For example, the result of transformation?
1
by: BW | last post by:
I am creating an upload/download function for an extranet site. Files will be uploaded to directory based upon the users login and associated project. The function works as long as I use "c:\Temp"...
4
by: Brian Parker | last post by:
Here's a snippet of code I have: ============================================== DataSet ds = new DataSet(); string strXMLFileName = Path.GetTempFileName(); StreamWriter sw = File.AppendText(...
29
by: jaysherby | last post by:
I'm new at Python and I need a little advice. Part of the script I'm trying to write needs to be aware of all the files of a certain extension in the script's path and all sub-directories. Can...
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
18
by: dainova | last post by:
Hi, I need to append a contents of the temp file to another output file, tried copy, but it doesn't work. I can't directly print into the output file as I need to check contents of temp first. I...
2
by: sajidali | last post by:
i am trying to append a text file using c#. when i executes my application i writes to the text file using more then one functions. i want to append file only during execution of programe. next time...
17
by: gayathrib1 | last post by:
i want to append a file(say g1) and add info frm temp file (say g2) .... the info frm g2 must be added at a paticular line of the file g1 say frm third line .... can anyone help me in this....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.