By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Rewriting the file

P: n/a
Is it possible to rewrite the entire file (to delete it's content) without
knowing the exact path?

#include <fstream>

void f(std::ostream file)
{
// ...
}
Thank you,
Zeljko
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Zeljko Knezevic wrote:
: void f(std::ostream file)

void f(std::ofstream file)
Zeljko

Jul 22 '05 #2

P: n/a
On Sun, 14 Dec 2003 19:38:28 +0100, "Zeljko Knezevic"
<ze*************@fer.hr.fer.hr> wrote in comp.lang.c++:
Is it possible to rewrite the entire file (to delete it's content) without
knowing the exact path?

#include <fstream>

void f(std::ostream 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
Jul 22 '05 #3

P: n/a
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
Jul 22 '05 #4

P: n/a
Zeljko Knezevic wrote:
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

Jul 22 '05 #5

P: n/a
"Zeljko Knezevic" <ze*************@fer.hr.fer.hr> wrote in message
news:br**********@bagan.srce.hr...
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::ofstream 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
Jul 22 '05 #6

P: n/a
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
Jul 22 '05 #7

P: n/a
Mike Wahler wrote:
: std::ofstream 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
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.