ofstream and rdstate()

From: Edo (edwardoJE_at_aking.com)
Date: 06/29/04

  • Next message: Russell Hanneken: "Re: int -> hex string"
    Date: Tue, 29 Jun 2004 03:58:40 -0700
    
    

    Hello
    I am trying to check for the existence of a file name in a given
    directory before I attempt to create this file, I don't want to replace
    the file if it is already there.
    I don't know HowTo, returning the ios::noreplace rdstate() is not the
    same as not attempting the operation if the file exists from the start.

    Thanks

    // generate a Studnet file with random grades for
    // accelarated C++ exercise 5-2
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;

    #include <fstream>
    using std::ofstream;

    #include <string>
    using std::string;

    int main()
    {
         // get number of lines from user
       cout << "Please indicate number of lines you want in a "
       "student file: ";
       int num;
       cin >> num;
       // get a name for the file
       cout << "Enter a name for this file: ";
       string s;
       cin >> s;

       // if file exists, ask for a different name, well, thats all I know
       ofstream oFile(s+".txt", ios::noreplace);
       if (oFile.rdstate() == ios::failbit){
           cout << "File already exists\n"
           "Please choose another name: ";
           cin >> s;
       }

       cout << "\n\n----- CREATED -----\n"
       "C:\\Some_Path" << s << endl
       << "Number of lines: " << num << endl;

       system("PAUSE");
       return 0;
    }


  • Next message: Russell Hanneken: "Re: int -> hex string"

    Relevant Pages

    • Re: Calling dll functions
      ... I call the above dll function from a C++ app. ... public static extern int DLLFunction(int Num, string Info); ... public static extern int DLLFunction(out int Num, ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Calling dll functions
      ... I call the above dll function from a C++ app. ... public static extern int DLLFunction(int Num, string Info); ... public static extern int DLLFunction(out int Num, ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: concatenation
      ... | string s = "john" ... # include <string> ... Oss << Value; ...
      (alt.comp.lang.learn.c-cpp)
    • Re: parsing text
      ... >>string parse(string ss, int num){ ... > find does not return an iterator. ... > using namespace std; ...
      (alt.comp.lang.learn.c-cpp)
    • Re: integer to characters
      ... Alan wrote: ... char *package(unsigned int num, char *s) ... Now give us an exercise from the *second* chapter of your Introduction to C ...
      (alt.comp.lang.learn.c-cpp)