Frustration - Input Output Question

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

    Frustration - Input Output Question

    Hi,

    I have a C/C++ program..

    I'm using a certain file to dump lots of computed numbers in it because
    I might need to check them later on...so the only way I was able to do
    that is by "appending" all the data to that file...because if I use the
    "write" ("w") function..every time the iteration takes place it will
    delete the previous data.

    if I run the program X number of times...that file is just being
    appended..and its size is becoming huge...and I have to always remember
    to delete the old file before I run my program again in case I needed
    to check those number..

    so my question is: is there a way I can delete the old file only when I
    start executing the program again....I mean I want to dump all the data
    of one run in that file..but when I run the program again I want to
    delete the old file...and let it recreate it again...

    what I think that it would help..is that I introduce a line where I
    delete that file at the beginning when I am initializing all my
    original data and then when the program hits its usual point the file
    is then recreated and the data appended...

    how can I do that, is there such a thing in C or C++ (C
    preferrably)... thank you for your help...

    Freddy

  • Richard G. Riley

    #2
    Re: Frustration - Input Output Question

    On 2006-02-24, Freddy <zfreddyzzz@gma il.com> wrote:[color=blue]
    > Hi,
    >
    > I have a C/C++ program..
    >
    > I'm using a certain file to dump lots of computed numbers in it because
    > I might need to check them later on...so the only way I was able to do
    > that is by "appending" all the data to that file...because if I use the
    > "write" ("w") function..every time the iteration takes place it will
    > delete the previous data.
    >
    > if I run the program X number of times...that file is just being
    > appended..and its size is becoming huge...and I have to always remember
    > to delete the old file before I run my program again in case I needed
    > to check those number..
    >
    > so my question is: is there a way I can delete the old file only when I
    > start executing the program again....I mean I want to dump all the data
    > of one run in that file..but when I run the program again I want to
    > delete the old file...and let it recreate it again...
    >[/color]

    If I have read correctly it is as simple as:

    You want to recreate/reset the file every time you start your
    program:

    use fopen at the start with the appropriate flag at the start of the
    program. "w". It truncates the file or creates it if it wasnt already
    there. You can then keep using this for appending later using the same
    FILE object.

    The key here is, if I understand you, is that you must keep using this
    SAME "FILE *" for later file writes : do not close it until you are
    finished. It sounds to me that you are reopening and closing the file
    in your computational loops in append mode ("a") without recreating it
    at the start of your program.

    Good luck!

    Comment

    • Walter Roberson

      #3
      Re: Frustration - Input Output Question

      In article <1140798215.620 197.137130@i40g 2000cwc.googleg roups.com>,
      Freddy <zfreddyzzz@gma il.com> wrote:[color=blue]
      >I have a C/C++ program..[/color]

      No such thing... it is either a C program or a C++ program, but not both.
      [color=blue]
      >so my question is: is there a way I can delete the old file only when I
      >start executing the program again.[/color]

      remove() the file by name, paying attention to race conditions
      according to the security risks associated with your program.

      [color=blue]
      >I'm using a certain file to dump lots of computed numbers in it because
      >I might need to check them later on...so the only way I was able to do
      >that is by "appending" all the data to that file...because if I use the
      >"write" ("w") function..every time the iteration takes place it will
      >delete the previous data.[/color]

      Only if something about your iterations close the file between
      interations and you are reopening. If that is the case and
      -somehow- you are able to figure out that a particular run is the
      first run of a sequence, then for that first run open with "w"
      and for the other runs open with a+ or ab+. Then you don't
      have to remove() the file.
      --
      Prototypes are supertypes of their clones. -- maplesoft

      Comment

      • Jordan Abel

        #4
        Re: Frustration - Input Output Question

        On 2006-02-24, Walter Roberson <roberson@ibd.n rc-cnrc.gc.ca> wrote:[color=blue]
        > In article <1140798215.620 197.137130@i40g 2000cwc.googleg roups.com>,
        > Freddy <zfreddyzzz@gma il.com> wrote:[color=green]
        >>I have a C/C++ program..[/color]
        >
        > No such thing... it is either a C program or a C++ program, but not both.[/color]

        <OT>
        I believe the C++ standard does define linkage between C translation
        units and C++ ones.
        </OT>

        Comment

        • Emmanuel Delahaye

          #5
          Re: Frustration - Input Output Question

          Freddy a écrit :[color=blue]
          > I have a C/C++ program..[/color]

          I assume you meant a program with parts written in C and parts written
          in some other language(s).
          [color=blue]
          > I'm using a certain file to dump lots of computed numbers in it because
          > I might need to check them later on...[/color]

          Sounds reasonable.
          [color=blue]
          > so the only way I was able to do
          > that is by "appending" all the data to that file...because if I use the
          > "write" ("w") function..every time the iteration takes place it will
          > delete the previous data.[/color]

          Sounds reasonable.
          [color=blue]
          > if I run the program X number of times...that file is just being
          > appended..and its size is becoming huge...and I have to always remember
          > to delete the old file before I run my program again in case I needed
          > to check those number..[/color]

          Could be automated. You have the standard remove() function for the purpose.

          --
          C is a sharp tool

          Comment

          Working...