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

Problem with fstream

P: n/a
I am trying to open a file in in+out+binary mode, and if the file
does not exist I want to create it.. this is the snippit of code
I am using:

fHistory.open(filename.c_str(),
std::ios::in | std::ios::out | std::ios::binary);

if (!fHistory.is_open()) {
fHistory.open(filename.c_str(),
std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
}

if (!fHistory.is_open()) {
throw CException("Error creating " + filename);
}

fHistory.write("Test", 4);

If I delete the file specified in "filename", compile and run, it
creates a file of zero bytes; if I run again, it writes the block
"Test" to the file. I'm not sure what I'm doing wrong or overlooking,
but if someone could shed some light, I would greatly appreciate it!

Thanks!

--
Kristofer Pettijohn
kr*******@cybernetik.net
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I'm not sure what you want. Do you want to truncate an existing file if
there is one and open a new file otherwise? Are you closing any file
opened?

Fraser.
Jul 22 '05 #2

P: n/a
Fraser Ross <fraserATmembers.v21.co.unitedkingdom> wrote:
I'm not sure what you want. Do you want to truncate an existing file if
there is one and open a new file otherwise? Are you closing any file
opened?


I want to create a file if it does not exist, and if it exists, I want
to open it for random seeking with binary reads and writes.

I used ios::trunc to create the file if it did not exist, since there
is no similar way to have fstream create a file "if it doesn't exist"
as there is in C with the fopen flag "a+" (which is what I am trying
to replicate).

--
Kristofer Pettijohn
kr*******@cybernetik.net
Jul 22 '05 #3

P: n/a
> I used ios::trunc to create the file if it did not exist, since there
is no similar way to have fstream create a file "if it doesn't exist"
as there is in C with the fopen flag "a+" (which is what I am trying
to replicate).


std::trunc is an open mode for existing files which does nothing for
non-existing files. If a file doesn't exist the a+ append flag would be
meaningless.

Fraser.
Jul 22 '05 #4

P: n/a
Kristofer Pettijohn <kr*******@cybernetik.net> wrote:
If I delete the file specified in "filename", compile and run, it
creates a file of zero bytes; if I run again, it writes the block
"Test" to the file. I'm not sure what I'm doing wrong or overlooking,
but if someone could shed some light, I would greatly appreciate it!


FWIW: I forgot to call fHistory.clear() after the first open failed..
all works well now..

void History::openHistory(void) {
std::cerr << "Opening history file " << filename << std::endl;

fHistory.open(filename.c_str(),
std::ios::in | std::ios::out | std::ios::binary |
std::ios::ate);

if (!fHistory.is_open()) {
fHistory.clear();
fHistory.open(filename.c_str(), std::ios::in | std::ios::out | std::ios:
:binary | std::ios::trunc);
if (!fHistory.is_open()) {
throw CException("Error creating " + filename);
}
}
}

--
Kristofer Pettijohn
kr*******@cybernetik.net
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.