seemingly simple ofstream problem please help

Discussion in 'C++' started by Tom Johnson, Aug 15, 2003.

  1. Tom Johnson

    Tom Johnson Guest

    Hi all, I cant believe that Im stuck on such simple code, but I am
    so...
    Im trying to open a text file for writing but can never get the file
    to initially open. Heres what Im having trouble with.

    -----------------------------------------------
    #include <iostream.h>
    #include <fstream.h>

    void main()
    {
    ofstream file ("C:\inventory\example.txt")
    if (!file.is_open)cout<<"not open";
    }
    ------------------------------------------------
    Ive tried ios::eek:ut, ios::app, etc... but I just keep getting "not
    open" in the console. Its been 3 years since I learned and used
    Visual C++ and I just picked it up again yesterday, so am I forgetting
    something? Any help would be greatly appreciated. Thanks!
    Tom Johnson
    Tom Johnson, Aug 15, 2003
    #1
    1. Advertising

  2. "Tom Johnson" <> wrote in message
    news:...
    > Hi all, I cant believe that Im stuck on such simple code, but I am
    > so...
    > Im trying to open a text file for writing but can never get the file
    > to initially open. Heres what Im having trouble with.
    >
    > -----------------------------------------------
    > #include <iostream.h>
    > #include <fstream.h>
    >
    > void main()
    > {
    > ofstream file ("C:\inventory\example.txt")


    ofstream file ("C:\\inventory\\example.txt")

    Easy mistake to make.

    john
    John Harrison, Aug 15, 2003
    #2
    1. Advertising

  3. "John Harrison" <> wrote in message
    news:bhis2h$21n0$-berlin.de...
    >
    > "Tom Johnson" <> wrote in message
    > news:...
    > > Hi all, I cant believe that Im stuck on such simple code, but I am
    > > so...
    > > Im trying to open a text file for writing but can never get the file
    > > to initially open. Heres what Im having trouble with.
    > >
    > > -----------------------------------------------
    > > #include <iostream.h>
    > > #include <fstream.h>
    > >
    > > void main()
    > > {
    > > ofstream file ("C:\inventory\example.txt")

    >
    > ofstream file ("C:\\inventory\\example.txt")
    >
    > Easy mistake to make.
    >
    > john
    >


    Incidentally there are several other things wrong with your code, I'm sure
    it because you've been away for three years.

    1) There are no header files called <fstream.h> and <iostream.h>, the
    correct header files are <iostream> and <fstream>. The .h versions are
    probably implementing a non-standard legacy version of the iostream library
    (I'm taking a wild guess at your compiler being VC++). You should switch to
    the standard library.

    2) ofstream, cout and similar standard names are in the std namespace.

    std::eek:fstream file ("C:\\inventory\\example.txt");

    std::cout << "not open";

    3) main returns an int, not void.

    john
    John Harrison, Aug 15, 2003
    #3
  4. Tom Johnson

    red floyd Guest

    Tom Johnson wrote:
    > Hi all, I cant believe that Im stuck on such simple code, but I am
    > so...
    > Im trying to open a text file for writing but can never get the file
    > to initially open. Heres what Im having trouble with.
    >
    > -----------------------------------------------
    > #include <iostream.h>
    > #include <fstream.h>
    >
    > void main()
    > {
    > ofstream file ("C:\inventory\example.txt")
    > if (!file.is_open)cout<<"not open";
    > }
    > ------------------------------------------------
    > Ive tried ios::eek:ut, ios::app, etc... but I just keep getting "not
    > open" in the console. Its been 3 years since I learned and used
    > Visual C++ and I just picked it up again yesterday, so am I forgetting
    > something? Any help would be greatly appreciated. Thanks!
    > Tom Johnson



    #include <iostream>
    #include <fstream>

    int main()
    {
    std::eek:fstream file ("C:\\inventory\\example.txt");
    if (!file.is_open()) std::cout << "not open" << std::endl;
    return 0;
    }
    red floyd, Aug 15, 2003
    #4
  5. Tom Johnson

    red floyd Guest

    red floyd wrote:

    > Tom Johnson wrote:
    >

    [redacted]
    >
    >
    > #include <iostream>
    > #include <fstream>
    >
    > int main()
    > {
    > std::eek:fstream file ("C:\\inventory\\example.txt");
    > if (!file.is_open()) std::cout << "not open" << std::endl;
    > return 0;
    > }
    >


    Actually, that second line in main() should just read:

    if (!file) std::cout << ...
    red floyd, Aug 15, 2003
    #5
    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. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    595
    Ivan Johansen
    Jul 14, 2005
  2. Zilla
    Replies:
    12
    Views:
    488
    Zilla
    Oct 23, 2007
  3. Guilherme Polo

    Re: seemingly simple list indexing problem

    Guilherme Polo, Jul 28, 2008, in forum: Python
    Replies:
    3
    Views:
    270
    John Krukoff
    Jul 30, 2008
  4. W.Sh
    Replies:
    5
    Views:
    115
    Luke Matuszewski
    Feb 11, 2006
  5. ThePacific
    Replies:
    0
    Views:
    363
    ThePacific
    Sep 26, 2012
Loading...

Share This Page