473,498 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

input/output to text file

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
Jul 22 '05 #1
3 3086
"qbschmidt" <ms******@unitizing.com> wrote in message
news:fc**************************@posting.google.c 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
Jul 22 '05 #2
"lallous" <la*****@lgwm.org> wrote in message
news:2f************@uni-berlin.de...
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
Jul 22 '05 #3
"lallous" <la*****@lgwm.org> wrote in message news:<2f************@uni-berlin.de>...
"qbschmidt" <ms******@unitizing.com> wrote in message
news:fc**************************@posting.google.c 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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2428
by: cherico | last post by:
from popen2 import popen2 r, w = popen2 ( 'tr "" ""' ) w.write ( t ) # t is a text file of around 30k bytes w.close () text = r.readlines () print text r.close () This simple script halted...
7
7521
by: MM | last post by:
Hi there, How can I change my code (below) so that I use an "input argument" to specify the file name of the input file? For example, if I compile the code and that the application then gets the...
1
1828
by: Aalok | last post by:
This is what i want to do. Read a text file as an input and based on that file, Create an outpu text file which saves the contents of the input file in a specifi format in the output file. I...
4
6189
by: Carlo Marchesoni | last post by:
I really don't achieve to read a simple 'input.txt' with the following content: Jürg (Hex: 4a fc 72 67) to an identical 'output.txt' I do the following (and tried with tons of different...
3
2836
by: kvnsmnsn | last post by:
I've written the following Javascript file that includes an input text field and an output text field, the latter of which is initialized to zero. Each time the user enters a number in the input...
9
3922
by: seep | last post by:
hi i m finding following error on the code that i wants to use to get all record from table via store procedure with paging. the error is : Input string was not in a correct...
1
2649
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
14
12769
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my windows applicationm, i need to excute a batch file. this batch file throws some text and questions to the screen, i need to catch the standard Output, check if it's a question, in...
0
2683
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, I started a thread regarding this issue, but now i'm facing new problem, so i'm starting a new thread, hope it's ok... anyway, using my c# windows application, i'm excuting and exe file...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.