Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > opening file - file does not exist but no error

Reply
Thread Tools

opening file - file does not exist but no error

 
 
ben
Guest
Posts: n/a
 
      02-24-2004
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;
}


 
Reply With Quote
 
 
 
 
Jorge Rivera
Guest
Posts: n/a
 
      02-24-2004
ben wrote:
> 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)
> printf("cant open %s",file_name);
> else
> printf("\nfile %s opened succesfully\n",file_name);
>
> infile.close();
> return 0;
> }
>
>

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-24-2004

"Jorge Rivera" <> wrote in message
news:nzy_b.83371$%...
> ben wrote:
> > 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>


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


 
Reply With Quote
 
Nick Hounsome
Guest
Posts: n/a
 
      02-24-2004

"Jorge Rivera" <> wrote in message
news:nzy_b.83371$%...
> ben wrote:
> > 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/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?

> > printf("cant open %s",file_name);
> > else
> > printf("\nfile %s opened succesfully\n",file_name);
> >
> > infile.close();
> > return 0;
> > }
> >
> >



 
Reply With Quote
 
Nick Hounsome
Guest
Posts: n/a
 
      02-24-2004

"John Harrison" <> wrote in message
news:c1f1ml$1i5enr$...
>
> "Jorge Rivera" <> wrote in message
> news:nzy_b.83371$%...
> > ben wrote:
> > > 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>

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


> john
>
>



 
Reply With Quote
 
Nick Hounsome
Guest
Posts: n/a
 
      02-24-2004

"ben" <> wrote in message
news:JDw_b.43333$...
> 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.

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



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-24-2004
> >
> > 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


That's not true. There is no mention of <iostream.h> (for instance) in the
C++ standard.

> 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


 
Reply With Quote
 
Nick Hounsome
Guest
Posts: n/a
 
      02-24-2004

"Nick Hounsome" <> wrote in message
news:5uF_b.5538$...
>
> "Jorge Rivera" <> wrote in message
> news:nzy_b.83371$%...
> > ben wrote:
> > > 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/iostrea...torvoidpt.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.

> 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?
>
> > > printf("cant open %s",file_name);
> > > else
> > > printf("\nfile %s opened succesfully\n",file_name);
> > >
> > > infile.close();
> > > return 0;
> > > }
> > >
> > >

>
>



 
Reply With Quote
 
Jorge Rivera
Guest
Posts: n/a
 
      02-24-2004

> 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.
 
Reply With Quote
 
Chris Mantoulidis
Guest
Posts: n/a
 
      02-25-2004
> #include <iostream.h>
> #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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CS0234 Global does not exist ... but it genuinely does Bill Johnson ASP .Net Datagrid Control 0 07-08-2005 06:34 PM
CS0234 Global does not exist ... but it genuinely does Bill Johnson ASP .Net 0 07-08-2005 06:34 PM
Word file not opening from Adm -My recent docs but opening from file menu of Word Thaqalain Computer Support 0 06-30-2005 02:20 AM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. Èý¹â ASP .Net 1 07-29-2003 04:31 PM
How do you figure out the LDAP://? ("Error authenticating. Error authenticating user. The specified domain either does not exist or could not be contacted") mrwoopey ASP .Net 3 06-30-2003 10:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57