Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with fstream

Reply
Thread Tools

Problem with fstream

 
 
Kristofer Pettijohn
Guest
Posts: n/a
 
      08-19-2004
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:ut | std::ios::binary);

if (!fHistory.is_open()) {
fHistory.open(filename.c_str(),
std::ios::in | std::ios:ut | 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Fraser Ross
Guest
Posts: n/a
 
      08-20-2004
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.


 
Reply With Quote
 
 
 
 
Kristofer Pettijohn
Guest
Posts: n/a
 
      08-20-2004
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
(E-Mail Removed)
 
Reply With Quote
 
Fraser Ross
Guest
Posts: n/a
 
      08-20-2004
> 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.


 
Reply With Quote
 
Kristofer Pettijohn
Guest
Posts: n/a
 
      08-22-2004
Kristofer Pettijohn <(E-Mail Removed)> 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:penHistory(void) {
std::cerr << "Opening history file " << filename << std::endl;

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

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

--
Kristofer Pettijohn
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is different between <fstream.h> and <fstream>MS VC++ Armando C++ 6 01-29-2004 09:01 AM
problem with fstream Frédéric Manzanares C++ 3 11-29-2003 10:13 PM
file creation problem in Windows using fstream Brandon McCombs C++ 8 11-24-2003 08:46 AM
Fstream problem jbruno4000 C++ 5 11-03-2003 04:30 AM
fstream problem las C++ 1 07-16-2003 12:09 PM



Advertisments