opening file - file does not exist but no error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ben

    opening file - file does not exist but no error

    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(fil e_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

    #2
    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(fil e_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]

    Comment

    • John Harrison

      #3
      Re: opening file - file does not exist but no error


      "Jorge Rivera" <jorgeri@roches ter.rr.com> wrote in message
      news:nzy_b.8337 1$%72.49644@twi ster.nyroc.rr.c om...[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


      Comment

      • Nick Hounsome

        #4
        Re: opening file - file does not exist but no error


        "Jorge Rivera" <jorgeri@roches ter.rr.com> wrote in message
        news:nzy_b.8337 1$%72.49644@twi ster.nyroc.rr.c om...[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(fil e_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

        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]


        Comment

        • Nick Hounsome

          #5
          Re: opening file - file does not exist but no error


          "John Harrison" <john_andronicu s@hotmail.com> wrote in message
          news:c1f1ml$1i5 enr$1@ID-196037.news.uni-berlin.de...[color=blue]
          >
          > "Jorge Rivera" <jorgeri@roches ter.rr.com> wrote in message
          > news:nzy_b.8337 1$%72.49644@twi ster.nyroc.rr.c om...[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]


          Comment

          • Nick Hounsome

            #6
            Re: opening file - file does not exist but no error


            "ben" <ben@ben.com> wrote in message
            news:JDw_b.4333 3$fW.9787@twist er.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(fil e_name);
            >
            > if (infile.fail())
            > printf("cant open %s",file_name );
            > else
            > printf("\nfile %s opened succesfully\n", file_name);
            >
            > infile.close();
            > return 0;
            > }
            >
            >[/color]


            Comment

            • John Harrison

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


              Comment

              • Jorge Rivera

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

                Comment

                • Chris Mantoulidis

                  #9
                  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(fil e_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

                  Comment

                  • Old Wolf

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

                    Comment

                    Working...