Rewriting the file

Z

Zeljko Knezevic

Is it possible to rewrite the entire file (to delete it's content) without
knowing the exact path?

#include <fstream>

void f(std::eek:stream file)
{
// ...
}


Thank you,
Zeljko
 
Z

Zeljko Knezevic

Zeljko Knezevic wrote:
: void f(std::eek:stream file)

void f(std::eek:fstream file)


Zeljko
 
J

Jack Klein

Is it possible to rewrite the entire file (to delete it's content) without
knowing the exact path?

#include <fstream>

void f(std::eek:stream file)
{
// ...
}


Thank you,
Zeljko

To do anything at all to a file in a C++ program, you need to provide
a C string (an array of characters) to the .open member function of a
stream, or the C functions fopen(), remove(), or rename().

Exactly what constitutes a valid string representation of a file is
completely platform specific, as in general the format of tile names
is defined by the operating system, not by a language.

In some cases, some operating systems allow files to be named by
partial names, generally if they are in the "current directory" of the
executing program.

But if you need specific information about what your specific compiler
and operating system combination requires or allows you to do, you
need to post in a group that supports your particular combination. It
is not something defined by the C++ language.

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Z

Zeljko Knezevic

Jack Klein wrote:
[...]

I believe I was misunderstood. The file I was referring to is already opened
in the function main(). The function f() gets only the pointer to the file,
not the path itself. Is it possible for that function to rewrite that file?


Zeljko
 
T

Thomas Matthews

Zeljko said:
Jack Klein wrote:
[...]

I believe I was misunderstood. The file I was referring to is already opened
in the function main(). The function f() gets only the pointer to the file,
not the path itself. Is it possible for that function to rewrite that file?


Zeljko
Yes, one can rewind the file and start _overwriting_ the
existing data with new data.
Methods for rewinding:
rewind()
fstream::seekp()
fstream::seekg()
fseek()


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
M

Mike Wahler

Zeljko Knezevic said:
Jack Klein wrote:
[...]

I believe I was misunderstood. The file I was referring to is already opened
in the function main(). The function f() gets only the pointer to the file,
not the path itself. Is it possible for that function to rewrite that

Yes of course. All the file operations(depending upon which
form is being used, 'C file streams' or 'C++ IOStreams') use
either a type 'FILE*' argument or a 'stream' type argument
which identifies the file.

std::eek:fstream file("xyz"); /* open file named "xyz" */
file << "Hello"; /* write to file designated by stream name 'file' */

Read the documentation of the 'open mode' flags to learn how to
control whether a file is 'truncated' before writing, appended to,
etc. This 'open mode' is given either as the constructor argument,
or as an argument to the stream's 'open' member function.

BTW which C++ book(s) are you reading?

-Mike
 
Z

Zeljko Knezevic

Thomas Matthews wrote:
: Yes, one can rewind the file and start _overwriting_ the
: existing data with new data.

Yes, that crossed my mind. But during overwriting, I'll be dropping out some
data so the file will be smaller. By simply overwriting existing data with
the new data I will end up with wasted space on the end of the file. I guess
I'll have to make an intermediate file and send two pointers to the
function: one to the file containing intermediate data and another to the
blank file in which I'll save my final data.


Thank you for your assistance,
Zeljko
 
Z

Zeljko Knezevic

Mike Wahler wrote:
: std::eek:fstream file("xyz"); /* open file named "xyz" */

Correct. But I'd like not to send the file path to the function, but the
file handler only. My function has no idea how my file is named on the disk
(or other storage device) and/or where it is located.

Never mind, I guess I'll have to send the path as well in order to close the
file and then reopen it using appropriate mode in order to delete it's
content.

: BTW which C++ book(s) are you reading?

It's been a couple of years since I read a C++ book. The one I read hasn't
been translated to English so I guess you surely haven't heard for it. Is
there any title you can come up with which is dealing with C++ and you'd
recommend? I'd like the examples in it to be OOP rather than C-style
programming.


Thank you for your assistance,
Zeljko
 

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

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,405
Latest member
DavidCex

Latest Threads

Top