473,398 Members | 2,525 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,398 software developers and data experts.

change values in a file

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
Nov 14 '05 #1
5 1745
John Smith wrote:
<snip> 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.


Don't you need to know all the original values in order to compute the
average correctly?
Nov 14 '05 #2
On Mon, 16 May 2005 01:38:45 GMT, John Smith <js****@company.com>
wrote in comp.lang.c:
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>.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
In article <42********@dnews.tpgi.com.au>,
Arafangion <Ar********@invalid.email.address.com> wrote:
John Smith wrote:
<snip> 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.


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.

Nov 14 '05 #4
Kenny McCormack wrote:
In article <42********@dnews.tpgi.com.au>,
Arafangion <Ar********@invalid.email.address.com> wrote:
John Smith wrote:
<snip> 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.


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.

I didn't see any current number of observations in the original description.
Nov 14 '05 #5
In article <42******@dnews.tpgi.com.au>,
Arafangion <Ar********@invalid.email.address.com> wrote:
Kenny McCormack wrote:
In article <42********@dnews.tpgi.com.au>,
Arafangion <Ar********@invalid.email.address.com> wrote:
John Smith wrote:
<snip> 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.

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.

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.

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cecil Westerhof | last post by:
With global.asa application variables can be set. Later can a script in your application change the value of those variables. But when IIS is restarted the old values are used again. Is there a way...
2
by: Sami | last post by:
I keep getting the famous 'Too Many Connection' Error, and don't know how to change my variables, so they persist even if i have to restart mysql service. Platform: Windows 2003 Server My...
5
by: Mortimer | last post by:
Hi, I hope someone can help. I can't seem to find the answer to this anywhere. I need to change the SQL terminator from a semicolon (;) to an exclamation point within SPUFI in order to process a...
2
by: James Dean | last post by:
I create a bitmap like this. The width and height are got from the compressed file i am reading. The width and height are in pixels.....1bpp bitmap = new...
3
by: Frankie | last post by:
I'm writing a small utility app (C# Windows forms) used to create new ASP.NET Web sites. This utility needs to be able to change existing values in an existing Web.config. Please note: I do NOT...
13
by: nyt | last post by:
I have a problem of number and text field. I got the database file(mdb) that contains many combo boxes used and its list values are created by "value list" For eg field Field name= 'furniture'...
1
by: Iwan Petrow | last post by:
Hi, I have dynamic properties (for checkboxes Checked property which I use in Option dialog box) . After I change checkboxes check properties I use DOM to save the changed values in the config...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
4
jamesd0142
by: jamesd0142 | last post by:
Ok here's one for the experts... I want to create a text file "test.text" that stores different values on each line. Is it possible for me to go to the directory its saved and change the...
6
by: Andrew Jocelyn | last post by:
Hi How do I programmatically change (read/write) the values in this app.config file at runtime? Specifically I want to change the client endpoint address but it would be nice to change other...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.