Problem with fstream

  • Thread starter Kristofer Pettijohn
  • Start date
K

Kristofer Pettijohn

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::eek:ut | std::ios::binary);

if (!fHistory.is_open()) {
fHistory.open(filename.c_str(),
std::ios::in | std::ios::eek: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!
 
F

Fraser Ross

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.
 
K

Kristofer Pettijohn

Fraser Ross said:
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).
 
F

Fraser Ross

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.
 
K

Kristofer Pettijohn

Kristofer Pettijohn said:
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::eek:penHistory(void) {
std::cerr << "Opening history file " << filename << std::endl;

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

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

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

Similar Threads

Problem mixing read and write on io fstream 1
fstream - write a file 3
std::fstream::seekp( ... std::ios::end ) 2
problem with fstream 3
writing binary file (ios::binary) 9
fstream File i/o 1
fstream.. 3
fstream 6

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top