change values in a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    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
  • Arafangion

    #2
    Re: change values in a file

    John Smith wrote:
    <snip> I would like to read in the name and average,[color=blue]
    > re-calculate the new average based on the latest test score and
    > write the new average to the file.[/color]

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

    Comment

    • Jack Klein

      #3
      Re: change values in a file

      On Mon, 16 May 2005 01:38:45 GMT, John Smith <jsmith@company .com>
      wrote in comp.lang.c:
      [color=blue]
      > 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[/color]

      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.l earn.c-c++

      Comment

      • Kenny McCormack

        #4
        Re: change values in a file

        In article <4287fa8c$1@dne ws.tpgi.com.au> ,
        Arafangion <Arafangion@inv alid.email.addr ess.com> wrote:[color=blue]
        >John Smith wrote:
        ><snip> I would like to read in the name and average,[color=green]
        >> re-calculate the new average based on the latest test score and
        >> write the new average to the file.[/color]
        >
        >Don't you need to know all the original values in order to compute the
        >average correctly?[/color]

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

        Comment

        • Arafangion

          #5
          Re: change values in a file

          Kenny McCormack wrote:[color=blue]
          > In article <4287fa8c$1@dne ws.tpgi.com.au> ,
          > Arafangion <Arafangion@inv alid.email.addr ess.com> wrote:
          >[color=green]
          >>John Smith wrote:
          >><snip> I would like to read in the name and average,
          >>[color=darkred]
          >>>re-calculate the new average based on the latest test score and
          >>>write the new average to the file.[/color]
          >>
          >>Don't you need to know all the original values in order to compute the
          >>average correctly?[/color]
          >
          >
          > No. You just need to know the current average and the (current) number of
          > observations. You do the math.
          >[/color]
          I didn't see any current number of observations in the original description.

          Comment

          • Kenny McCormack

            #6
            Re: change values in a file

            In article <42882c82@dnews .tpgi.com.au>,
            Arafangion <Arafangion@inv alid.email.addr ess.com> wrote:[color=blue]
            >Kenny McCormack wrote:[color=green]
            >> In article <4287fa8c$1@dne ws.tpgi.com.au> ,
            >> Arafangion <Arafangion@inv alid.email.addr ess.com> wrote:
            >>[color=darkred]
            >>>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?[/color]
            >>
            >>
            >> No. You just need to know the current average and the (current) number of
            >> observations. You do the math.
            >>[/color]
            >I didn't see any current number of observations in the original description.[/color]

            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.

            Comment

            Working...