Re: adding colums to text



pete wrote:
Barry Schwarz wrote:
On Sat, 28 Jun 2008 23:11:13 -0400, pete <pfiland@xxxxxxxxxxxxxx>
wrote:

Bill Cunningham wrote:
I have a row of values like such, placed in a text file by fprintf.

10.50
10.25
10.00
10.75
11.00

What I want to do to the above colum is add a new column right beside it which is a total of these values and then average them in another column. For example.

11.00 52.50

In another column is the average of the 5.

11.00 52.50 10.50

I am assuming that fprintf is going to have to be used and maybe freopen.
Text files and linked lists, go together like hot dogs and mustard.

/* BEGIN new.c output */

File is open for writing.
File lines:
10.50
10.25
10.00
10.75
11.00
File is closed.

File is open for reading.
Reading file into a linked list...
File is closed.

snip code

Why on earth would you needlessly complicate such a simple task with
linked lists, especially given BC's previous posting history?

I posted that, because that's really how I would do it.
The first two things that popped into my head
were "linked list" and "extra temp file".
My preference is to always minimize the number of open files.


/* BEGIN new.c output */

File is open for writing.
File is closed.
File is open for reading.
File lines:
10.50
10.25
10.00
10.75
11.00
File is rewound.

Calculating sum and average.
sum is 52.500000. average is 5.250000
File is rewound.

Temp file is open for writing.
Reading file into a temp file...
File is closed.
Temp file is closed.

Temp file is open for reading.
File is open for writing.
File is closed.
Temp file is closed.
Temp file is removed.

File is open for reading.
File lines:
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
11.00 52.50 5.25
File is closed.
File is removed.

/* END new.c output */

I just noticed that the resulting file is wrong.


--
pete
.



Relevant Pages