Hi,
I have a code to open a file like this,
int r = open("file1", O_RDONLY, 0);
let's say the executable a.out is at <dir> and file1 is also at
<dir>, then no problem.
The problem is when my current directory is at <dir>/<sub_dir> and
file1 is at the sub directory, but a.out is still at <dir>, then when I
run this command,
../a.out
a.out always pick up <dir>/file1, instead of <dir>/<sub_dir>/file1.
For some reason it is impossible to change the open() statement
above, is there any solution to let open() pick up file1 from where I
run a.out, instead of where a.out sits?
Thanks. 5 1585
John Black wrote: Hi, I have a code to open a file like this,
int r = open("file1", O_RDONLY, 0);
let's say the executable a.out is at <dir> and file1 is also at <dir>, then no problem.
The problem is when my current directory is at <dir>/<sub_dir> and file1 is at the sub directory, but a.out is still at <dir>, then when I run this command,
../a.out
a.out always pick up <dir>/file1, instead of <dir>/<sub_dir>/file1.
For some reason it is impossible to change the open() statement above, is there any solution to let open() pick up file1 from where I run a.out, instead of where a.out sits?
None of this has anything to do with the C programming
language, which has no open() function nor any notion of
directories, subdirectories, or a current directory.
Try comp.unix.programmer <off-topic> and be prepared to
provide more than just one line of code, because the behavior
you're seeing is not the behavior POSIX specifies, which
means there's an error elsewhere in your program. </off-topic>
-- Er*********@sun.com
"John Black" <bl***@eed.com> wrote in message For some reason it is impossible to change the open() statement above, is there any solution to let open() pick up file1 from where I run a.out, instead of where a.out sits?
You should use fopen(). It is standard and only rarely do you need to use
the non-ANSI functions.
What I am saying applies to the string passed to fopen(). I am pretty sure
it also applies to open(), but I don't have docs with me.
C has no directory functions. The system will decide what your "current
directory" is and how to open a file passed as "foo.txt". Almost always you
can specify a definite file by passing an absolute path , such as
"root/xdir/ydir/foo.txt". Unfortunately you cannot contruct such a path,
using ANSI functions alone. However there are always non-standard directory
functions, which you can use to do this. In your case you probably want to
search for a directory of a specific name that is close to your current
directory, so you could do that by 1) recursively searching for
subdirectories with the right name below your current directory, 2) if that
fails, going up a level and repeating the search. 3) once you've found it,
construct an absolute path.
John Black wrote: Hi, I have a code to open a file like this,
int r = open("file1", O_RDONLY, 0);
There's no such function in the C language. It is a POSIX function. If
you are using UNIX (as seems probable from your message), then a good
place to ask is comp.unix.programmer.
For some reason it is impossible to change the open() statement above
What does this mean? Why are you unable to change it?
is there any solution to let open() pick up file1 from where I run a.out, instead of where a.out sits?
Again, anything related to paths will be implementation-specific. You
need a newsgroup for your platform.
Brian Rodenborn
Eric Sosman <Er*********@sun.com> writes: John Black wrote: Hi, I have a code to open a file like this, int r = open("file1", O_RDONLY, 0); let's say the executable a.out is at <dir> and file1 is also at <dir>, then no problem. The problem is when my current directory is at <dir>/<sub_dir> and file1 is at the sub directory, but a.out is still at <dir>, then when I run this command, ../a.out a.out always pick up <dir>/file1, instead of <dir>/<sub_dir>/file1. For some reason it is impossible to change the open() statement above, is there any solution to let open() pick up file1 from where I run a.out, instead of where a.out sits?
None of this has anything to do with the C programming language, which has no open() function nor any notion of directories, subdirectories, or a current directory.
Try comp.unix.programmer <off-topic> and be prepared to provide more than just one line of code, because the behavior you're seeing is not the behavior POSIX specifies, which means there's an error elsewhere in your program. </off-topic>
<off-topic>... like a chdir() somewhere else in your program, or a
misunderstanding of what your program is actually doing.</off-topic>
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
John Black wrote: Hi, I have a code to open a file like this,
int r = open("file1", O_RDONLY, 0);
let's say the executable a.out is at <dir> and file1 is also at <dir>, then no problem.
The problem is when my current directory is at <dir>/<sub_dir> and file1 is at the sub directory, but a.out is still at <dir>, then when I run this command,
../a.out
a.out always pick up <dir>/file1, instead of <dir>/<sub_dir>/file1.
For some reason it is impossible to change the open() statement above, is there any solution to let open() pick up file1 from where I run a.out, instead of where a.out sits?
Thanks.
This is not a C question. We are Off Topic here.
Do the 'pwd' command to see where you really are.
Use cd to get to <dir>/<sub_dir>.
Do 'ls file1' to see if it's really there.
Do 'ls ../a.out' for the same reason.
Do '../a.out' to run <dir>/a.out against <dir>/<sub_dir>/file1.
You really need to read more.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein --- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jofio |
last post by:
Hello,
I am a newbie in PHP and I am enthusiastically trying out things. I've
just replaced my
index.html (
file with
index.php
|
by: Jonathan Driller |
last post by:
I am very new to this and would greatly appreciate some insight.
I am trying to learn Python by doing something useful; write a script
that will count and output my aggregated visits to my website....
|
by: Sven Groot |
last post by:
Using Outlook 2002 NL with all updates installed.
I'm trying to open a backup pst file I have on CD. I have copied the file to
my harddrive, made sure it's not read-only, am logged in as...
|
by: Ian Rastall |
last post by:
I've got a small search page at http://www.sundry.ws/search.html and
I would like to add a section that allows me to open files on my
computer, instead of having to go to File > Open. It's just...
|
by: Nikos |
last post by:
Hi... I would like to search for a hex string (for example: "E903") inside a
binary file...
Although I open the file correctly, how do I search hex values?
Thanks in advance!
Nikos
|
by: PengYu.UT |
last post by:
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();...
|
by: Anoop |
last post by:
Hi,
I am new to this newsgroup and need help in the following questions.
1. I am workin' on a GUI application. Does C# provides Layout Managers the
way Java does to design GUI? I know that it...
|
by: TtfnJohn |
last post by:
I have two small scripts that while on the surface should both work
the problem is they don't.
Here's the first one:
import re
testString = 'Thap,fpvi,*!wtyd@*.dip.t-dialin.net:*!ylx@*.dip.t-...
|
by: dprjessie |
last post by:
Hello,
I am a Web programmer and I'm working on my first desktop application as a favor for a friend. I'm sure I have a stupid error here, but there is no error being thrown so I can't figure out...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
| |