opening file - file does not exist but no error

B

ben

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;
}
 
J

Jorge Rivera

ben said:
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())

Truy replacing this for
if(infile)
 
J

John Harrison

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
 
N

Nick Hounsome

Jorge Rivera said:
ben said:
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())

Truy replacing this for
if(infile)

I left my std at work but according to an online reference at
http://www.cplusplus.com/ref/iostream/ios/operatorvoidpt.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?
 
N

Nick Hounsome

John Harrison said:
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.

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

Nick Hounsome

ben said:
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;

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

John Harrison

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.

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
 
N

Nick Hounsome

Nick Hounsome said:
Jorge Rivera said:
ben said:
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())

Truy replacing this for
if(infile)

I left my std at work but according to an online reference at
http://www.cplusplus.com/ref/iostream/ios/operatorvoidpt.html
This should be the same as fail() i.e. badbit or failbit

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

Jorge Rivera

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.

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

Chris Mantoulidis

#include said:
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>

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)
int main()
{
ifstream infile;
char file_name[16];

Doesn't your compiler have std::string? std::string is easier to use
than char[].
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);

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
infile.close();
return 0;
}

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
 
O

Old Wolf

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>

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.
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top