input/output to text file

Discussion in 'C++' started by lallous, May 6, 2004.

  1. lallous

    lallous Guest

    "qbschmidt" <> wrote in message
    news:...
    > i can input and output to a text file as long as the file is in the
    > same directory as my c++ program. but i cant figure out how to
    > input/output to a file in another directory i.e. I:\c++projects
    > please help.
    > thanks


    Hello,

    Specify the full path instead of just the file name, as:

    fstream f("i:\\c++projects\\yourfile.txt", ios::binary);

    --
    Elias
    lallous, May 6, 2004
    #1
    1. Advertising

  2. lallous

    qbschmidt Guest

    i can input and output to a text file as long as the file is in the
    same directory as my c++ program. but i cant figure out how to
    input/output to a file in another directory i.e. I:\c++projects
    please help.
    thanks
    qbschmidt, May 6, 2004
    #2
    1. Advertising

  3. lallous

    Siemel Naran Guest

    "lallous" <> wrote in message
    news:...

    > Specify the full path instead of just the file name, as:
    >
    > fstream f("i:\\c++projects\\yourfile.txt", ios::binary);


    You can also usually also use .. as in

    fstream f("..\\lib\\yourfile.txt", ios::binary);

    But these types of details are outside the scope of C++, which knows nothing
    about directories. Note that these directories are usually relative to: (1)
    if running from the command line then the directory from which you ran the
    program (which may be different from the directory the program lives in),
    (2) in Windows the the Start In property which you can see when you right
    click the file.

    To avoid harcoding directory names you can use the the standard C function
    getenv to get an environment variable by name. But this design requires the
    existence of an environment variable, which comes with its own hassles,
    especially in the install script. You can also require the user to specify
    a config file in the command line which you can retrieve from argv. The
    config file often hard codes absolute directory names.

    int main(int argc, char * * argv);

    int main(int argc, char * * argv, char * * env); // the 3rd argument 'env'
    is non-standard, but many compilers support it
    Siemel Naran, May 7, 2004
    #3
  4. "lallous" <> wrote in message news:<>...
    > "qbschmidt" <> wrote in message
    > news:...
    > > i can input and output to a text file as long as the file is in the
    > > same directory as my c++ program. but i cant figure out how to
    > > input/output to a file in another directory i.e. I:\c++projects
    > > please help.
    > > thanks

    >
    > Hello,
    >
    > Specify the full path instead of just the file name, as:
    >
    > fstream f("i:\\c++projects\\yourfile.txt", ios::binary);


    Note the presence of \\. \c is an illegal excape sequence.
    "i:\temp" means "i: emp" because \t is a tab.

    You can also use "i:/c++projects/yourfile.txt". This may
    be surprising to users, but forward slashes work on more
    systems. (not on all, though. File systems aren't standard.)

    Regards,
    Michiel Salters
    Michiel Salters, May 7, 2004
    #4
    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. Emel
    Replies:
    2
    Views:
    5,848
    nish a
    Jan 29, 2010
  2. Guy
    Replies:
    5
    Views:
    2,814
    brucie
    Dec 13, 2003
  3. cjl
    Replies:
    1
    Views:
    10,491
    Michael Winter
    Oct 31, 2005
  4. ComicCaper
    Replies:
    2
    Views:
    336
    ComicCaper
    May 10, 2006
  5. Walter
    Replies:
    6
    Views:
    231
    Bob Barrows [MVP]
    Sep 17, 2004
Loading...

Share This Page