Frustration - Input Output Question

F

Freddy

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..everytime 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
 
R

Richard G. Riley

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..everytime 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...

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!
 
W

Walter Roberson

I have a C/C++ program..

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

remove() the file by name, paying attention to race conditions
according to the security risks associated with your 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..everytime the iteration takes place it will
delete the previous data.

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.
 
J

Jordan Abel

No such thing... it is either a C program or a C++ program, but not both.

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

Emmanuel Delahaye

Freddy a écrit :
I have a C/C++ program..

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

Sounds reasonable.
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..everytime the iteration takes place it will
delete the previous data.

Sounds reasonable.
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..

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

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,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top