Connecting Tech Pros Worldwide Forums | Help | Site Map

opening file - file does not exist but no error

ben
Guest
 
Posts: n/a
#1: Jul 22 '05
Hello,

This really drives me nuts.
Im opening an input file, and Im testing if it was open successfully. Thats
it!
I know that the file that Im trying to open DOES NOT exist but Im getting NO
error message.
No matter what name I enter I get 'file opened successfully"
Any clues? Thanks!

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>

int main()
{
ifstream infile;
char file_name[16];

printf("\nEnter file name: ");
cin>>file_name;

infile.open(file_name);

if (infile.fail())
printf("cant open %s",file_name);
else
printf("\nfile %s opened succesfully\n",file_name);

infile.close();
return 0;
}



Jorge Rivera
Guest
 
Posts: n/a
#2: Jul 22 '05

re: opening file - file does not exist but no error


ben wrote:[color=blue]
> Hello,
>
> This really drives me nuts.
> Im opening an input file, and Im testing if it was open successfully. Thats
> it!
> I know that the file that Im trying to open DOES NOT exist but Im getting NO
> error message.
> No matter what name I enter I get 'file opened successfully"
> Any clues? Thanks!
>
> #include <iostream.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include <fstream.h>
>
> int main()
> {
> ifstream infile;
> char file_name[16];
>
> printf("\nEnter file name: ");
> cin>>file_name;
>
> infile.open(file_name);
>
> if (infile.fail())[/color]

Truy replacing this for
if(infile)[color=blue]
> printf("cant open %s",file_name);
> else
> printf("\nfile %s opened succesfully\n",file_name);
>
> infile.close();
> return 0;
> }
>
>[/color]
John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

re: opening file - file does not exist but no error



"Jorge Rivera" <jorgeri@rochester.rr.com> wrote in message
news:nzy_b.83371$%72.49644@twister.nyroc.rr.com...[color=blue]
> ben wrote:[color=green]
> > Hello,
> >
> > This really drives me nuts.
> > Im opening an input file, and Im testing if it was open successfully.[/color][/color]
Thats[color=blue][color=green]
> > it!
> > I know that the file that Im trying to open DOES NOT exist but Im[/color][/color]
getting NO[color=blue][color=green]
> > error message.
> > No matter what name I enter I get 'file opened successfully"
> > Any clues? Thanks!
> >
> > #include <iostream.h>
> > #include <stdlib.h>
> > #include <stdio.h>
> > #include <fstream.h>[/color][/color]

Also try replacing the above with the standard

#include <iostream> // no .h
#include <stdlib.h>
#include <stdio.h>
#include <fstream> // no .h
using namespace std;

Depends on your compiler but you have more chance of getting standard
behaviour if you use the standard header files.

john


Nick Hounsome
Guest
 
Posts: n/a
#4: Jul 22 '05

re: opening file - file does not exist but no error



"Jorge Rivera" <jorgeri@rochester.rr.com> wrote in message
news:nzy_b.83371$%72.49644@twister.nyroc.rr.com...[color=blue]
> ben wrote:[color=green]
> > Hello,
> >
> > This really drives me nuts.
> > Im opening an input file, and Im testing if it was open successfully.[/color][/color]
Thats[color=blue][color=green]
> > it!
> > I know that the file that Im trying to open DOES NOT exist but Im[/color][/color]
getting NO[color=blue][color=green]
> > error message.
> > No matter what name I enter I get 'file opened successfully"
> > Any clues? Thanks!
> >
> > #include <iostream.h>
> > #include <stdlib.h>
> > #include <stdio.h>
> > #include <fstream.h>
> >
> > int main()
> > {
> > ifstream infile;
> > char file_name[16];
> >
> > printf("\nEnter file name: ");
> > cin>>file_name;
> >
> > infile.open(file_name);
> >
> > if (infile.fail())[/color]
>
> Truy replacing this for
> if(infile)[/color]

I left my std at work but according to an online reference at
http://www.cplusplus.com/ref/iostrea...torvoidpt.html
This should be the same as fail() i.e. badbit or failbit
An alternative would be to check for !good() as good includes eofbit
The online ref had nothing to say about the specific behaviour of open which
is interesting in itself.
P.S. do you get failbit when you actually try to use it?
[color=blue][color=green]
> > printf("cant open %s",file_name);
> > else
> > printf("\nfile %s opened succesfully\n",file_name);
> >
> > infile.close();
> > return 0;
> > }
> >
> >[/color][/color]


Nick Hounsome
Guest
 
Posts: n/a
#5: Jul 22 '05

re: opening file - file does not exist but no error



"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:c1f1ml$1i5enr$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "Jorge Rivera" <jorgeri@rochester.rr.com> wrote in message
> news:nzy_b.83371$%72.49644@twister.nyroc.rr.com...[color=green]
> > ben wrote:[color=darkred]
> > > Hello,
> > >
> > > This really drives me nuts.
> > > Im opening an input file, and Im testing if it was open successfully.[/color][/color]
> Thats[color=green][color=darkred]
> > > it!
> > > I know that the file that Im trying to open DOES NOT exist but Im[/color][/color]
> getting NO[color=green][color=darkred]
> > > error message.
> > > No matter what name I enter I get 'file opened successfully"
> > > Any clues? Thanks!
> > >
> > > #include <iostream.h>
> > > #include <stdlib.h>
> > > #include <stdio.h>
> > > #include <fstream.h>[/color][/color]
>
> Also try replacing the above with the standard>
> #include <iostream> // no .h
> #include <stdlib.h>
> #include <stdio.h>
> #include <fstream> // no .h
> using namespace std;
>
> Depends on your compiler but you have more chance of getting standard
> behaviour if you use the standard header files.
>[/color]

You keep harping on about this but the .h files are standard too even if
deprecated
and every implementation is always going to define one in terms of the other
to avoid the maintenance cost of two parrallel implementations - not to
mention the
difficulty in differentiating when linking.

[color=blue]
> john
>
>[/color]


Nick Hounsome
Guest
 
Posts: n/a
#6: Jul 22 '05

re: opening file - file does not exist but no error



"ben" <ben@ben.com> wrote in message
news:JDw_b.43333$fW.9787@twister.rdc-kc.rr.com...[color=blue]
> Hello,
>
> This really drives me nuts.
> Im opening an input file, and Im testing if it was open successfully.[/color]
Thats[color=blue]
> it!
> I know that the file that Im trying to open DOES NOT exist but Im getting[/color]
NO[color=blue]
> error message.
> No matter what name I enter I get 'file opened successfully"
> Any clues? Thanks!
>
> #include <iostream.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include <fstream.h>
>
> int main()
> {
> ifstream infile;
> char file_name[16];
>
> printf("\nEnter file name: ");
> cin>>file_name;[/color]

Please do not ever do this again - Microsoft issues at least one
critical patch a month because of
buffer overrun vulnerabilities due to people reading an unbounded string
into a fixed length array.

use a std::string - its much simpler and cannot be overrun.
[color=blue]
>
> infile.open(file_name);
>
> if (infile.fail())
> printf("cant open %s",file_name);
> else
> printf("\nfile %s opened succesfully\n",file_name);
>
> infile.close();
> return 0;
> }
>
>[/color]


John Harrison
Guest
 
Posts: n/a
#7: Jul 22 '05

re: opening file - file does not exist but no error


> >[color=blue][color=green]
> > Also try replacing the above with the standard>
> > #include <iostream> // no .h
> > #include <stdlib.h>
> > #include <stdio.h>
> > #include <fstream> // no .h
> > using namespace std;
> >
> > Depends on your compiler but you have more chance of getting standard
> > behaviour if you use the standard header files.
> >[/color]
>
> You keep harping on about this but the .h files are standard too even if
> deprecated[/color]

That's not true. There is no mention of <iostream.h> (for instance) in the
C++ standard.
[color=blue]
> and every implementation is always going to define one in terms of the[/color]
other[color=blue]
> to avoid the maintenance cost of two parrallel implementations - not to
> mention the
> difficulty in differentiating when linking.[/color]

That's not true either, Visual C++ 6 does maintain two parallel
implementations, and many posters to comp.lang.c++ have problems for exactly
that reason.

john


Jorge Rivera
Guest
 
Posts: n/a
#8: Jul 22 '05

re: opening file - file does not exist but no error


[color=blue]
> I checked it today and it explicitly says it should set failbit.
> It also says indirectly that it should set it if fopen would have returned
> NULL
> which it obviously would if the file doesn't exist.
>[/color]

Thanks for clarifying the standard. Just remember that some
implementations are broken, and maybe the alternative will work for him.
I just said, "try this," not "here you go,"

Regards,

Jorge L.
Chris Mantoulidis
Guest
 
Posts: n/a
#9: Jul 22 '05

re: opening file - file does not exist but no error


> #include <iostream.h>[color=blue]
> #include <stdlib.h>
> #include <stdio.h>
> #include <fstream.h>[/color]

use

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>

and

using namespace std;

so that there will be compatibility with your code (that is not to
type the std:: prefix)
[color=blue]
> int main()
> {
> ifstream infile;
> char file_name[16];[/color]

Doesn't your compiler have std::string? std::string is easier to use
than char[].
[color=blue]
> printf("\nEnter file name: ");
> cin>>file_name;
>
> infile.open(file_name);
>
> if (infile.fail())
> printf("cant open %s",file_name);
> else
> printf("\nfile %s opened succesfully\n",file_name);[/color]

There's the mistake. fail() checks for a fail bit, not if a file open
procedure has failed. So replace the above four lines with (note that
I prefer to use cout than printf; here's a C++ newsgroup):

if (!infile)
cout << "Can't open " << file_name << '\n'; //or (std::)endl
else
cout << "\nFile " << file_name << " opened successfully.\n"; //or
(std::)endl
[color=blue]
> infile.close();
> return 0;
> }[/color]

If this is all of your code (I doubt it though) then there's no need
to include <iostream> (in your implementation) or in my implementation
there's no need to include <cstdio>. In both implementations there's
no need to include <cstdlib>.

- cmad
Old Wolf
Guest
 
Posts: n/a
#10: Jul 22 '05

re: opening file - file does not exist but no error


> This really drives me nuts.[color=blue]
> Im opening an input file, and Im testing if it was open successfully. Thats
> it!
> I know that the file that Im trying to open DOES NOT exist but Im getting NO
> error message.
> No matter what name I enter I get 'file opened successfully"
> Any clues? Thanks!
>
> #include <iostream.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include <fstream.h>
>[/color]

What compiler and version are you using. Judging by your use of
pre-1998 headers and your symptoms, I'd guess a pretty old one.
Probably it does not implement the C++ Standard properly.
You will have to either upgrade compiler, or consult your
compiler's own documentation (supplied with it, or a relevant
newsgroup) on how to check for file-open success.
Closed Thread