opening file - file does not exist but no error

Discussion in 'C++' started by ben, Feb 24, 2004.

  1. ben

    ben Guest

    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;
    }
    ben, Feb 24, 2004
    #1
    1. Advertising

  2. ben

    Jorge Rivera Guest

    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;
    > }
    >
    >
    Jorge Rivera, Feb 24, 2004
    #2
    1. Advertising

  3. "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
    John Harrison, Feb 24, 2004
    #3
  4. "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/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?

    > > printf("cant open %s",file_name);
    > > else
    > > printf("\nfile %s opened succesfully\n",file_name);
    > >
    > > infile.close();
    > > return 0;
    > > }
    > >
    > >
    Nick Hounsome, Feb 24, 2004
    #4
  5. "John Harrison" <> wrote in message
    news:c1f1ml$1i5enr$-berlin.de...
    >
    > "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
    >
    >
    Nick Hounsome, Feb 24, 2004
    #5
  6. "ben" <> wrote in message
    news:JDw_b.43333$-kc.rr.com...
    > 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;
    > }
    >
    >
    Nick Hounsome, Feb 24, 2004
    #6
  7. > >
    > > 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
    John Harrison, Feb 24, 2004
    #7
  8. "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/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.

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

    >
    >
    Nick Hounsome, Feb 24, 2004
    #8
  9. ben

    Jorge Rivera Guest


    > 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.
    Jorge Rivera, Feb 24, 2004
    #9
  10. > #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
    Chris Mantoulidis, Feb 25, 2004
    #10
  11. ben

    Old Wolf Guest

    > 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.
    Old Wolf, Feb 25, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Johnson
    Replies:
    0
    Views:
    1,217
    Bill Johnson
    Jul 8, 2005
  2. LT
    Replies:
    7
    Views:
    2,071
    Phlip
    Jul 25, 2004
  3. Emre  Sevinc
    Replies:
    1
    Views:
    455
  4. Bill Johnson

    CS0234 Global does not exist ... but it genuinely does

    Bill Johnson, Jul 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    180
    Bill Johnson
    Jul 8, 2005
  5. David Beckwith
    Replies:
    5
    Views:
    182
    David Beckwith
    Oct 1, 2008
Loading...

Share This Page