Re: just a simple question re: ifstream

From: tlmseven (dabawenyo_at_gmail.com)
Date: 11/07/04


Date: 7 Nov 2004 07:51:04 -0800


"Jonathan Turkanis" <technews@kangaroologic.com> wrote in message news:<2v53aqF2g3la3U1@uni-berlin.de>...
> "tlmseven" <dabawenyo@gmail.com> wrote in message
> news:7443ffeb.0411061421.61258a2@posting.google.com...
> > 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

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.



Relevant Pages

  • Re: just a simple question re: ifstream
    ... >> I'm writing a simulator for my Comp Arch class, and I'm a bit rusty on ... >> We know that, when using ifstream to open a file for input, a file is ... >> opens a file that does exist? ... load a hex file of name "filename" obtained from user input: ...
    (comp.lang.cpp)
  • Re: just a simple question re: ifstream
    ... > We know that, when using ifstream to open a file for input, a file is ... opening a file for writing with an ofstream, in which case the default behavior ... use an fstream and open it in default mode. ... > opens a file that does exist? ...
    (comp.lang.cpp)
  • Re: who to use fwrite to write through a hidden file
    ... It opens a file, and NEGLECTS TO SEE IF THE OPEN WORKED! ... You have no reason to assume this could work. ... Let me put it this way: I would not consider writing such a piece of code. ... how can you be trusted to modify something as critical as ...
    (microsoft.public.vc.mfc)
  • Re: comparison with None
    ... This seems to go entirely against the spirit of the language. ... For example, if you expect an open file handle, but get a NoneType because you didn't really open a file, then it would be best to test the type of return object before using it. ... I am aware that this opens the gates to the hordes who will now pile in pointing out that this will technique will fail if f is zero or one of the many other items that evaluates to false in a Boolean context. ... It's that poor readability that Steven D'Aprano was making fun of in his remarks above, which in truth only echoes what I felt like but refrained from writing when I pointed out the redundancy in the ...
    (comp.lang.python)
  • Re: Behavior of fwrite(), fseek(), fflush() if the file gets deleted after being opened.
    ... I am writing an application in C with file handling. ... opens a fileusing fopenin binary write ... The C standard doesn't specify what happens when one process is access ... have an appropriate newsgroups line in your header for your mail to be seen, ...
    (comp.lang.c.moderated)