Why doesn't this code work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rudolfs.Bundulis@gmail.com

    #1

    Why doesn't this code work?

    As I understood it's better to use fstream instead of fstream.h. When I
    was porting my code (i'm using VC6.0++) i needed to let go of such
    things as ios::noreplace and ios::nocreate. I neede to check if file
    exists before creating it. If it exists we prompt that we are
    overwriting. The files ar being passed in as command line arguments.

    I did it like that:

    #include <fstream>

    std::fstream outFile;
    ....
    ....
    ....
    outFile.open(ar gv[2],std::ios_base: :in);// the second argument is the
    output file
    if (! outFile)
    {//file does not exist
    outFile.open(ar gv[2],std::ios::out) ;
    cout<<"Testing. ..";
    }
    else
    {
    cout<<"Warning: Output file already exists!\n";//prompt that it
    exists
    outFile.close() ;//close it
    outFile.open(ar gv[2],std::ios::out) ; //open it for output
    cout<<"Testing. ..";
    }
    outFile.close() ;

    but this stuff didn't work if the file did not exist. It just created
    an empty file;

    I changed it like this:

    std::ifstream tmpFile;
    std::ofstream outFile;
    ....
    ....
    tmpFile.open(ar gv[2],std::ios_base: :in);
    if (!tmpFile)
    {
    outFile.open(ar gv[2],std::ios_base: :out);
    cout<<"Testing. ..";

    }
    else
    {
    cout<<"Warning: Output file already exists!\n";
    tmpFile.close() ;
    outFile.open(ar gv[2],std::ios_base: :out);
    cout<<"Testing. ..";
    }
    outFile.close() ;

    this stuff works fine, but i would like to get rid of the tmpFile (if
    possible).
    Any ideas? Why didn' t the first one work?

  • Victor Bazarov

    #2
    Re: Why doesn't this code work?

    Rudolfs.Bunduli s@gmail.com wrote:
    As I understood it's better to use fstream instead of fstream.h. When
    I was porting my code (i'm using VC6.0++) i needed to let go of such
    things as ios::noreplace and ios::nocreate. I neede to check if file
    exists before creating it. If it exists we prompt that we are
    overwriting. The files ar being passed in as command line arguments.
    >
    I did it like that:
    >
    #include <fstream>
    >
    std::fstream outFile;
    ...
    ...
    ...
    outFile.open(ar gv[2],std::ios_base: :in);// the second argument is the
    output file
    if (! outFile)
    {//file does not exist
    outFile.open(ar gv[2],std::ios::out) ;
    cout<<"Testing. ..";
    }
    else
    {
    cout<<"Warning: Output file already exists!\n";//prompt that it
    exists
    outFile.close() ;//close it
    outFile.open(ar gv[2],std::ios::out) ; //open it for output
    cout<<"Testing. ..";
    }
    outFile.close() ;
    >
    but this stuff didn't work if the file did not exist. It just created
    an empty file;
    >
    I changed it like this:
    >
    std::ifstream tmpFile;
    std::ofstream outFile;
    ...
    ...
    tmpFile.open(ar gv[2],std::ios_base: :in);
    if (!tmpFile)
    {
    outFile.open(ar gv[2],std::ios_base: :out);
    cout<<"Testing. ..";
    >
    }
    else
    {
    cout<<"Warning: Output file already exists!\n";
    tmpFile.close() ;
    outFile.open(ar gv[2],std::ios_base: :out);
    cout<<"Testing. ..";
    }
    outFile.close() ;
    >
    this stuff works fine, but i would like to get rid of the tmpFile (if
    possible).
    Any ideas? Why didn' t the first one work?
    First off, you're using a rather old compiler/library implemenation.
    Perhaps you should consider switching to a more up-to-date release
    of their compiler/library.

    Second, if the behaviour is specific to VC++, you need to ask in the
    VC++ newsgroup (microsoft.publ ic.vc.language) .

    Third, when handling files (existence, etc.) it is recommended to use
    platform-specific mechanisms. I am sure you can always request the
    OS to give you the status of the file. Standard means (as you are
    finding out) are not very generic in those areas.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • BobR

      #3
      Re: Why doesn't this code work?


      Victor Bazarov wrote in message ...
      >Rudolfs.Bundul is@gmail.com wrote:
      >As I understood it's better to use fstream instead of fstream.h. When
      >I was porting my code (i'm using VC6.0++) i needed to let go of such
      >things as ios::noreplace and ios::nocreate. I neede to check if file
      >exists before creating it. If it exists we prompt that we are
      >overwriting. The files ar being passed in as command line arguments.
      >I did it like that:
      >>
      >#include <fstream>
      >...
      >I changed it like this:
      >>
      >std::ifstrea m tmpFile;
      >std::ofstrea m outFile;
      >...
      >tmpFile.open(a rgv[2],std::ios_base: :in);
      >if (!tmpFile){
      >outFile.open(a rgv[2],std::ios_base: :out);
      > cout<<"Testing. ..";
      >}
      >else{
      >cout<<"Warning : Output file already exists!\n";
      >tmpFile.close( );
      >outFile.open(a rgv[2],std::ios_base: :out);
      > cout<<"Testing. ..";
      >}
      >outFile.close( );
      >>
      >this stuff works fine, but i would like to get rid of the tmpFile (if
      >possible).
      >Any ideas? Why didn' t the first one work?
      >
      >
      >Third, when handling files (existence, etc.) it is recommended to use
      >platform-specific mechanisms.
      Ouch!

      [ from an old post ]
      // nwadc10 wrote:
      // I'm having trouble checking to see if a file already exists.
      // Use the stat method which is portable on Windows, UNIX, Linux, and most
      OS.

      // other includes here
      #include <sys/stat.h // hmmm, maybe not as portable??

      bool FileExist(char const *FileName){
      struct stat my_stat;
      return (stat(FileName, &my_stat) == 0);
      }

      bool IsDirectory(cha r const *FileName){
      struct stat my_stat;
      if(stat(FileNam e, &my_stat) != 0) return false;
      return ((my_stat.st_mo de & S_IFDIR) != 0);
      }

      // Many times, it's not just important to determind if the file exist, but
      // it's also important to determind if it a file or directory.
      // ----------------------------------------------------------------------


      // int main(int argc, char* argv[]){
      void FileExistMain(s td::ostream &cout = std::cout){
      // using std::cout // for NG posting
      bool v1 = FileExist("c:/autoexec.bat");
      bool v2 = FileExist("c:/nofile.bat");
      bool v3 = FileExist("c:/config.sys");
      bool v4 = FileExist("c:/nofile2.bat");
      bool v5 = IsDirectory("c:/windows");
      bool v6 = IsDirectory("c:/notA_dir");
      bool v7 = IsDirectory("c:/WINNT");

      cout<<"bool v1 = FileExist(\"c:\ \autoexec.bat\" ); ="<<v1<<std::en dl;
      cout<<"bool v2 = FileExist(\"c:\ \nofile.bat\"); ="<<v2<<std::en dl;
      cout<<"bool v3 = FileExist(\"c:\ \config.sys\"); ="<<v3<<std::en dl;
      cout<<"bool v4 = FileExist(\"c:\ \nofile2.bat\") ; ="<<v4<<std::en dl;
      cout<<"bool v5 = IsDirectory(\"c :\\windows\"); ="<<v5<<std::en dl;
      cout<<"bool v6 = IsDirectory(\"c :\\notA_dir\"); ="<<v6<<std::en dl;
      cout<<"bool v7 = IsDirectory(\"c :\\WINNT\"); ="<<v7<<std::en dl;
      return;
      }

      I did not save the posters name, so, I can not credit him/her.

      --
      Bob R
      POVrookie


      Comment

      • Marcus Kwok

        #4
        Re: Why doesn't this code work?

        BobR <RemoveBadBobR@ worldnet.att.ne twrote:
        [ from an old post ]
        // nwadc10 wrote:
        // I'm having trouble checking to see if a file already exists.
        // Use the stat method which is portable on Windows, UNIX, Linux, and most
        OS.
        >
        // other includes here
        #include <sys/stat.h // hmmm, maybe not as portable??
        >
        bool FileExist(char const *FileName){
        struct stat my_stat;
        return (stat(FileName, &my_stat) == 0);
        }
        >
        bool IsDirectory(cha r const *FileName){
        struct stat my_stat;
        if(stat(FileNam e, &my_stat) != 0) return false;
        return ((my_stat.st_mo de & S_IFDIR) != 0);
        }
        >
        // Many times, it's not just important to determind if the file exist, but
        // it's also important to determind if it a file or directory.
        // ----------------------------------------------------------------------
        >
        I did not save the posters name, so, I can not credit him/her.
        http://www.codecomments.com/archive3...10-664071.html
        seems to say that it was Axter.

        --
        Marcus Kwok
        Replace 'invalid' with 'net' to reply

        Comment

        • BobR

          #5
          Re: Why doesn't this code work?


          Marcus Kwok wrote in message ...
          >BobR wrote:
          >>
          >I did not save the posters name, so, I can not credit him/her.
          >
          >http://www.codecomments.com/archive3...10-664071.html
          >seems to say that it was Axter.
          Thank you, Marcus.
          [ I made note of that in my header file for future ref.. ]

          I have a slow connection and limited hours due to budget (un-employed). I
          appreciate your time & effort.

          Any comment on the portability of this header?
          #include <sys/stat.h // hmmm, maybe not as portable??

          --
          Bob R
          POVrookie


          Comment

          • Larry Smith

            #6
            Re: Why doesn't this code work?

            BobR wrote:
            Marcus Kwok wrote in message ...
            >BobR wrote:
            >>I did not save the posters name, so, I can not credit him/her.
            >http://www.codecomments.com/archive3...10-664071.html
            >seems to say that it was Axter.
            >
            Thank you, Marcus.
            [ I made note of that in my header file for future ref.. ]
            >
            I have a slow connection and limited hours due to budget (un-employed). I
            appreciate your time & effort.
            >
            Any comment on the portability of this header?
            #include <sys/stat.h // hmmm, maybe not as portable??
            >
            --
            Bob R
            POVrookie
            >
            >
            It's portable to most Unix/Linux versions.

            Comment

            • Marcus Kwok

              #7
              Re: Why doesn't this code work?

              BobR <RemoveBadBobR@ worldnet.att.ne twrote:
              >
              Marcus Kwok wrote in message ...
              >>BobR wrote:
              >>>
              >>I did not save the posters name, so, I can not credit him/her.
              >>
              >>http://www.codecomments.com/archive3...10-664071.html
              >>seems to say that it was Axter.
              >
              Thank you, Marcus.
              [ I made note of that in my header file for future ref.. ]
              >
              I have a slow connection and limited hours due to budget (un-employed). I
              appreciate your time & effort.
              It wasn't much effort really... since in the original post "determind"
              is misspelled :)
              Any comment on the portability of this header?
              #include <sys/stat.h // hmmm, maybe not as portable??
              AFAIK anything in sys/ is not a standard header in terms of C++, but
              maybe it is considered a standard POSIX header.

              --
              Marcus Kwok
              Replace 'invalid' with 'net' to reply

              Comment

              Working...