473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to open a file in $PATH?

The following code open the file "example.txt" in the current
directory.
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}

But I'm wondering how to code to support the search path such as
$AWK_PATH? That is to say if I want to open some file, if it is not
under the current directory, it will search all the directories in
$AWK_PATH until one is found. Is there any standard C++ package to
support this? Or is there any sample code?

Thanks,
Peng

Jan 22 '06 #1
6 5237

<Pe*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
The following code open the file "example.txt" in the current
directory.
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}

But I'm wondering how to code to support the search path such as
$AWK_PATH?
That is to say if I want to open some file, if it is not
under the current directory, it will search all the directories in
$AWK_PATH until one is found. Is there any standard C++ package to
support this? Or is there any sample code?


Not with only standard C++, since it has no notion of
'search path'. However it could be done with a bit of
help from a compiler and/or operating system that
features a 'search path'. Some operating systems
provide this with an 'environment variable'. Programmatic
access to such a variable is typically done with some non-
standard compiler-specific extension function such as 'getenv()'
(check your documentation). Once you've retrieved the string(s)
from such a 'path variable', you can use standard string-handling
functions to parse them, and use them as arguments to file-opening
functions.

Since this is a platform-dependent operation, I can't offer example
code for the 'search path' retrieval. However, once you've figured
how to do it on your system(s), if you still have trouble e.g.
isolating each path as a string, post the (standard) C++ code
you've tried, and we'll offer further assistance.

-Mike
Jan 22 '06 #2

Mike Wahler wrote:
<Pe*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
The following code open the file "example.txt" in the current
directory.
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}

But I'm wondering how to code to support the search path such as
$AWK_PATH?
That is to say if I want to open some file, if it is not
under the current directory, it will search all the directories in
$AWK_PATH until one is found. Is there any standard C++ package to
support this? Or is there any sample code?


Not with only standard C++, since it has no notion of
'search path'. However it could be done with a bit of
help from a compiler and/or operating system that
features a 'search path'. Some operating systems
provide this with an 'environment variable'. Programmatic
access to such a variable is typically done with some non-
standard compiler-specific extension function such as 'getenv()'
(check your documentation). Once you've retrieved the string(s)
from such a 'path variable', you can use standard string-handling
functions to parse them, and use them as arguments to file-opening
functions.

Since this is a platform-dependent operation, I can't offer example
code for the 'search path' retrieval. However, once you've figured
how to do it on your system(s), if you still have trouble e.g.
isolating each path as a string, post the (standard) C++ code
you've tried, and we'll offer further assistance.


The OS is linux.

Suppose I use ":" to separate directories, it seems the following like
is enough to split the string into multiple directory names.
http://groups.google.com/group/comp....f66cfee6b6c320
Is this way good enough?

Once I have the directory names, should I first check if the file is
exist by "stat"
http://groups.google.com/group/comp....43bcd8cbe72933
Is there any other way?

Thanks,
Peng

Jan 22 '06 #3
getenv() is in C++, since it was in C. It's in the <cstdlib> header.
Aside from that, there's no support for environment variables in C++.

Jan 22 '06 #4
A split function should work.

The "stat" is part of POSIX, and not C++. Instead, just open a file and
use the istream::good() function to determine whether the open
succeeded.

Jan 22 '06 #5
Mike Wahler wrote:
Some operating systems provide this with an 'environment variable'.
Programmatic access to such a variable is typically done with some non-
standard compiler-specific extension function such as 'getenv()'
(check your documentation).


getenv is nont a compiler-specific extension. It's part of the C++ standard
library.

Jan 23 '06 #6

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:dr*************@news.t-online.com...
Mike Wahler wrote:
Some operating systems provide this with an 'environment variable'.
Programmatic access to such a variable is typically done with some non-
standard compiler-specific extension function such as 'getenv()'
(check your documentation).


getenv is nont a compiler-specific extension. It's part of the C++
standard
library.


Oops, my mistake. Of course you're right.

-Mike
Jan 23 '06 #7

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

Similar topics

31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
2
by: kevcar40 | last post by:
Hi i am trying to open an excel file from access using a button the problem is the path and could change so i need to build the path to the file i am using textboxes to hold the information as...
6
by: Brad | last post by:
I have a win2003 server workstation with multiple webs, each web has it's own ip address. In VS2005, if I select to open an existing web site, select Local IIS, the dialog correctly displays a...
7
by: Jed | last post by:
I am trying to open web project in VS 2003 using the File Share method. VS is running on XP Pro (Host) and I am accessing the root web of an XP Pro install on Virtual PC (Server) running on the...
0
by: Niyazi | last post by:
Hi, I created application that store the data in SQL SERVER that reside on network. The client also use this application to access the resources provided with application. But is the client want...
24
by: dancer | last post by:
Using ASP.net 1.1 and Microsoft Access. I received the following error message. Why? I have closed the Access file. I have another very small access file that opens with no trouble with the...
0
by: Ofelia | last post by:
Hi, I'm new to this forum and to Perl language but I would like to ask for your help. I'm working in Linux and the files I need to process are in the format “file.gz”. I created a script which...
25
by: Andy_Khosravi | last post by:
I just recently changed my database that I'm running from a monolithic DB to a split FE/BE. The front end resides on the client machine and the BE resides on a network drive. I'm experimenting with...
1
by: sunnyluthra1 | last post by:
Hi Everyone, I am working on a piece of code, that displays the properties(Name, Datatype, size & Description) of the table in the database. Now I want to further Enhance the code. I Have created...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.