DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: verify a file opened

  1. #1
    newatit Guest

    verify a file opened


    I'm writing a program and have to verify that the file open command did succeed
    and if not the program will write a message to that effect and then terminate
    then we have to prompt the user for strings of data until the user wants
    to quit. Any suggestions on this would be greatly appreciated.


    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    #include <conio.h>

    main()
    { ofstream outPutFile("ATxtFile.dat", ios::app);


    char our_String[80];

    cout << "We will be adding data to textfile :";

    cout << "\n\nENTER a string (press ENTER only to quit): "
    << endl;
    cin.getline(our_String, 80, '\n');


    outPutFile << our_String
    << endl;


    outPutFile.close();

    return 0;
    }

  2. #2
    jonnin Guest

    Re: verify a file opened


    Its a boolean I think, maybe i am thinking FILE*, I forget.

    ofstream x;
    if(x.open("filename")
    {
    do ok stuff
    }
    else
    {
    do not ok stuff.
    }

    "newatit" <janraeevans@aol.com> wrote:
    >
    >I'm writing a program and have to verify that the file open command did

    succeed
    >and if not the program will write a message to that effect and then terminate
    >then we have to prompt the user for strings of data until the user wants
    >to quit. Any suggestions on this would be greatly appreciated.
    >
    >
    >#include <iostream.h>
    >#include <fstream.h>
    >#include <iomanip.h>
    >#include <stdlib.h>
    >#include <conio.h>
    >
    >main()
    >{ ofstream outPutFile("ATxtFile.dat", ios::app);
    >
    >
    > char our_String[80];
    >
    > cout << "We will be adding data to textfile :";
    >
    > cout << "\n\nENTER a string (press ENTER only to quit): "
    > << endl;
    > cin.getline(our_String, 80, '\n');
    >
    >
    > outPutFile << our_String
    > << endl;
    >
    >
    > outPutFile.close();
    >
    > return 0;
    >}



  3. #3
    Danny Kalev Guest

    Re: verify a file opened



    newatit wrote:
    >
    > I'm writing a program and have to verify that the file open command did succeed
    > and if not the program will write a message to that effect and then terminate
    > then we have to prompt the user for strings of data until the user wants
    > to quit. Any suggestions on this would be greatly appreciated.
    >
    > #include <iostream.h>
    > #include <fstream.h>
    > #include <iomanip.h>
    > #include <stdlib.h>
    > #include <conio.h>
    >
    > main()
    > { ofstream outPutFile("ATxtFile.dat", ios::app);

    simply check the stream object as if it were a bool:
    if(outPutFile)
    {
    / successfully opened
    }
    else
    {
    //error
    }

    Danny

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center