change values in a file

J

John Smith

I have a file which contains two columns, name of students and
average. I would like to read in the name and average,
re-calculate the new average based on the latest test score and
write the new average to the file. I know how to do the first two
parts (reading the name and average and calculating the new
average). How do I do the last part, i.e. write the new average to
the file?

here is a sample file
 
A

Arafangion

John Smith wrote:
re-calculate the new average based on the latest test score and
write the new average to the file.

Don't you need to know all the original values in order to compute the
average correctly?
 
J

Jack Klein

I have a file which contains two columns, name of students and
average. I would like to read in the name and average,
re-calculate the new average based on the latest test score and
write the new average to the file. I know how to do the first two
parts (reading the name and average and calculating the new
average). How do I do the last part, i.e. write the new average to
the file?

here is a sample file
---------------------------------
#comment: this is a sample file
student1 average1
student2 average2
student3 average3

This sounds like the general problem of replacing text in a text file,
which is addressed briefly in the FAQ for comp.lang.c, link in my
signature.

It is possible to do this directly if and only if the new text is
exactly the same length as the original text. File systems are not
like word processors or editors. They do not provide a method of
shoving the rest of the text down if you write more in the middle, or
pulling the rest of the file up if you take something out.

The standard way to do something like this is to create a new file for
writing with a temporary name, one you make up or one generated by the
standard tmpnam() function. Then open your original file for reading.

Read from the original file line-by-line, perhaps using fgets(), make
whatever modifications you need to the data, and write it to the new
file most likely with fputs() or fprintf().

When you have finished, fclose() both files. Either remove() or
rename() the original file (perhaps to some pattern used for back up
files), then rename() the new file to the name of the original.

This method handles modifications where deletions and additions are
different sizes or in different parts of the file.

All of the functions I mentioned are prototyped in <stdio.h>.
 
K

Kenny McCormack

John Smith wrote:


Don't you need to know all the original values in order to compute the
average correctly?

No. You just need to know the current average and the (current) number of
observations. You do the math.
 
A

Arafangion

Kenny said:
No. You just need to know the current average and the (current) number of
observations. You do the math.
I didn't see any current number of observations in the original description.
 
K

Kenny McCormack

I didn't see any current number of observations in the original description.

Wasn't really stated one way or the other.

But, maybe that's already known - say, if they (the students) get one test
a week, and we know (from looking at a calendar) how many weeks have gone
by.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,897
Members
47,439
Latest member
shasuze

Latest Threads

Top