rewind writing to a text file

L

Lynn McGuire

This is not a C++ question in itself, but it may be somewhat related.
If I fopen a file for concatenation, write a few things using fprintf
and then change my mind and want to throw away my changes to the file,
is there a way to do this in C or C++ ?

Thanks,
Lynn
 
D

Default User

Lynn McGuire said:
This is not a C++ question in itself, but it may be somewhat related.
If I fopen a file for concatenation, write a few things using fprintf
and then change my mind and want to throw away my changes to the file,
is there a way to do this in C or C++ ?

No. Your best bet is to open a temporary file, then only copy over the
original when sure.



Brian
 
L

Lynn McGuire

No. Your best bet is to open a temporary file, then only copy over the
original when sure.



Brian

It is a 50 MB log file on my web server. I would prefer not to
copy it if I don't have to.

I may rework the logging code to concatenate the strings to a
std::string and then write that to the log file at the end of
the transaction. But, I would have a LOT of code to rewrite.

Thanks,
Lynn
 
M

Miles Bader

Lynn McGuire said:
I may rework the logging code to concatenate the strings to a
std::string and then write that to the log file at the end of
the transaction. But, I would have a LOT of code to rewrite.

If your existing code writes to an iostream, can't you just switch the
target iostream to an std::eek:stringstream for a while, and then flush
_that_ string (or not) to the real logging stream?

-Miles
 
L

Lynn McGuire

Miles Bader said:
If your existing code writes to an iostream, can't you just switch the
target iostream to an std::eek:stringstream for a while, and then flush
_that_ string (or not) to the real logging stream?

-Miles

My existing code uses fopen and fprintf. No streams. I was hoping for
some way to cancel to writing to the file while I still had it open for
concatenation.

Thanks,
Lynn
 
M

Miles Bader

Lynn McGuire said:
My existing code uses fopen and fprintf. No streams. I was hoping for
some way to cancel to writing to the file while I still had it open for
concatenation.

Ugh, can't help you there....

If you're willing to be unportable, some platforms have extensions that
allow writing to memory using stdio streams (e.g., GNU libc has
"fmemopen"), which could be used similarly to std::eek:stringstream.

-Miles
 
L

Lynn McGuire

Ugh, can't help you there....

If you're willing to be unportable, some platforms have extensions that
allow writing to memory using stdio streams (e.g., GNU libc has
"fmemopen"), which could be used similarly to std::eek:stringstream.

-Miles

I guess that I will convert to using a std::string for my logfile
concatenation and only write it if I want to at the end of the
transaction. And, that will be portable.

Thanks,
Lynn
 
D

Default User

It is a 50 MB log file on my web server. I would prefer not to
copy it if I don't have to.

Then use some sort of memory buffering. Others have mentioned an iostream
approach. You could use a C-string approach, with sprintf to write to a temp
and only dump the string out when you reach a commit point. However, I just
can't help feeling you have a fundamental flaw in the program design if this
is even an issue.



Brian
 

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
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top