just a simple question re: ifstream

T

tlmseven

I'm writing a simulator for my Comp Arch class, and I'm a bit rusty on
my C++ so I was wondering if anyone could help me out.

We know that, when using ifstream to open a file for input, a file is
created by default if the specified filename in
ifstream("somefilename") does not exist.

How does one disable this default feature so that the program only
opens a file that does exist?

Thanks to those of you who are willing to help.
 
J

Jonathan Turkanis

tlmseven said:
I'm writing a simulator for my Comp Arch class, and I'm a bit rusty on
my C++ so I was wondering if anyone could help me out.

We know that, when using ifstream to open a file for input, a file is
created by default if the specified filename in
ifstream("somefilename") does not exist.

This is wrong. A file is never created in this case. Perhaps you are thinking of
opening a file for writing with an ofstream, in which case the default behavior
is to create a new file if one does not already exist.

To disable this behavior, use an fstream and open it in default mode. To force a
file to be created when using an fstream, use the flag ios::trunc.
How does one disable this default feature so that the program only
opens a file that does exist?

Thanks to those of you who are willing to help.

Jonathan
 
P

Peter Jansson

Hi,

Your basic assumption "We know that, when using ifstream to open a file
for input, a file is created by default if the specified filename in
ifstream("somefilename") does not exist." is not correct. An ifstream
means opening a file for reading only which is not so good if the file
does not exist already and you could check to see if the opened ifstream
is valid before doing anything with it.

Do you mean an ifstream or an ofstream?

Regards,
Peter Jansson
http://www.jansson.net/
 
T

tlmseven

Jonathan Turkanis said:
This is wrong. A file is never created in this case. Perhaps you are thinking of
opening a file for writing with an ofstream, in which case the default behavior
is to create a new file if one does not already exist.

To disable this behavior, use an fstream and open it in default mode. To force a
file to be created when using an fstream, use the flag ios::trunc.


Jonathan

Thanks, but it actually did happen me in runtime. Here's a sample of
my code. In simulating a PIC microcontroller, I write a function to
load a hex file of name "filename" obtained from user input:

void l(char filename[])
{
ifstream hexfile(filename);
....
}

When testing it, I entered a filename of a file that did not exist in
the default directory. The program proceeded to create a file of that
filename in the directory and attempted to read "nothing" from it -
since it was an empty file - and the program crashed.
 
J

John Harrison

tlmseven said:
Jonathan Turkanis said:
This is wrong. A file is never created in this case. Perhaps you are
thinking of
opening a file for writing with an ofstream, in which case the default
behavior
is to create a new file if one does not already exist.
[snip]

Thanks, but it actually did happen me in runtime. Here's a sample of
my code. In simulating a PIC microcontroller, I write a function to
load a hex file of name "filename" obtained from user input:

Either you are mistaken, or you are using a compiler that implements the
standard wrongly. If the latter then you better look up your compiler docs,
because standard C++ does not behave in that way.

What compiler are you using? Some compilers have a legacy versions of the
iostream library, make sure you are using the latest version of the iostream
library.

john
 
T

tlmseven

Jonathan Turkanis said:
This is wrong. A file is never created in this case. Perhaps you are thinking of
opening a file for writing with an ofstream, in which case the default behavior
is to create a new file if one does not already exist.

To disable this behavior, use an fstream and open it in default mode. To force a
file to be created when using an fstream, use the flag ios::trunc.


Jonathan

Well, the thing is it actually happened in real time. The program I'm
writing is a simulator for a PIC Chip and it prompts the user to enter
the filename of the hex file he/she would like to load into the
program. The code goes like:

void l(char filename[])
{
ifstream hexfile(filename[])
.....
}

Now, I input a filename that did not correspond to any file in the
default directory I was saving to. Although it was an ifstream that
was declared, the program created a new file and attempted to read
"nothing" from this new file and subsequently crashed.
 
T

tlmseven

Peter Jansson said:
Hi,

Your basic assumption "We know that, when using ifstream to open a file
for input, a file is created by default if the specified filename in
ifstream("somefilename") does not exist." is not correct. An ifstream
means opening a file for reading only which is not so good if the file
does not exist already and you could check to see if the opened ifstream
is valid before doing anything with it.

Do you mean an ifstream or an ofstream?

Regards,
Peter Jansson
http://www.jansson.net/

I apologize. Scratch the fact that I said "We know that..." I suppose
I came off as speaking in theoretical terms. But refer to what I typed
regarding what actually happened in runtime. Yeah, I was using an
ifstream to read a file for input. I am using Visual Studio 6.0
(Visual C++ compiler). Then, as I said:

"I input a filename that did not correspond to any file in the
default directory I was saving to. Although it was an ifstream that
was declared, the program created a new file and attempted to read
"nothing" from this new file and subsequently crashed."

Thanks for all your comments!
 
O

Old Wolf

Thanks, but it actually did happen me in runtime. Here's a sample of
my code. In simulating a PIC microcontroller, I write a function to
load a hex file of name "filename" obtained from user input:

void l(char filename[])
{
ifstream hexfile(filename);
...
}

When testing it, I entered a filename of a file that did not exist in
the default directory. The program proceeded to create a file of that
filename in the directory and attempted to read "nothing" from it -
since it was an empty file - and the program crashed.

How do you know the file was actually created? Can you see
it in the file system? (eg. using Explorer or a command prompt).

Also, it doesn't crash a program if you read from an empty file.
You must have made some other error (eg. reading from a file
handle that wasn't open, or using the results of a failed read).

Can you post a complete program that demonstrates the problem?
 

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

No members online now.

Forum statistics

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

Latest Threads

Top