Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > input/output to text file

Reply
Thread Tools

input/output to text file

 
 
lallous
Guest
Posts: n/a
 
      05-06-2004
"qbschmidt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
qbschmidt
Guest
Posts: n/a
 
      05-06-2004
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
 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      05-07-2004
"lallous" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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[i]. 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


 
Reply With Quote
 
Michiel Salters
Guest
Posts: n/a
 
      05-07-2004
"lallous" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "qbschmidt" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I tell the difference between the end of a text file, and an empty line in a text file? walterbyrd Python 7 05-17-2007 06:02 AM
Re: text file search to text file output possible? Whiskers Computer Support 3 10-07-2006 06:32 PM
Re: text file search to text file output possible? Mitch Computer Support 0 10-06-2006 11:15 PM
Controlling text in a Text Area or Text leo ASP General 1 12-05-2005 01:13 AM
Read Text File and split them to individual text file Krish ASP .Net 1 10-20-2005 03:39 PM



Advertisments