473,396 Members | 1,933 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,396 software developers and data experts.

Fopen to create a file in a sub-folder (such as "Mygoal").

Hi all,

Please tell me how to create a file (using fopen) in a sub-folder
(such as sub-folder "Mygoal")? I tried

if ( ( fp = fopen ( "mygoal\myfile.tif", "wb" ) ) == NULL ) {
fprintf ( stderr, "cannot open file %s\n");
exit ( 1 );
}

But it doesn't work.

Also , is there anyone know how to get the current directory from C
codes?

Thanks
Jan 29 '08 #1
8 5586
On Mon, 28 Jan 2008 17:46:54 -0800 (PST), user923005
<dc*****@connx.comwrote in comp.lang.c:
On Jan 28, 5:05*pm, VijaKhara <VijaKh...@gmail.comwrote:
Hi all,

Please tell me how to create a file (using fopen) in a sub-folder
(such as sub-folder "Mygoal")? I tried

if ( ( fp = fopen ( "mygoal\myfile.tif", "wb" ) ) == NULL ) {
* * fprintf ( stderr, "cannot open file %s\n");
* * exit ( 1 );
* }

But it doesn't work.

Look up errno return and call perror to find out what went wrong.
Besides Ben's comment, some compilers may now allow you to create a
file in a directory that does not exist.
Why lookup errno? fopen() is not required to set it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jan 29 '08 #2
user923005 wrote:
[...]
There are not any compilers that I know of that do not set the errno
value on fopen() failure.
But the fact that you are allowed to leave errno unchanged is clearly
a gaffe.
The fact that neither you nor I know of any systems which do not
set errno does not mean that there aren't any. I can imagine a
"security" system which would prohibit access to some files, but
for security reasons, refuse to tell you why. (ie: there is no
way to distinguish between "the file doesn't exist" and "the file
does exist, but you didn't give the correct encryption key".)

However, if I were on the committee, I would have suggested making
something like a generic EUNKNOWN error code, which could be used
on systems where a failure could occur, but for which the C runtime
could not determine the "true" cause. (Perhaps the O/S simply
returns a "failed" condition?) At least, you could then trust that
errno was set, even if you can't guarantee that it was set to a
"known" error code. For all I know, the suggestion was made, but
reject for some reason.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Jan 29 '08 #3
On Jan 29, 1:52*pm, Kenneth Brody <kenbr...@spamcop.netwrote:
user923005 wrote:

[...]
There are not any compilers that I know of that do not set the errno
value on fopen() failure.
But the fact that you are allowed to leave errno unchanged is clearly
a gaffe.

The fact that neither you nor I know of any systems which do not
set errno does not mean that there aren't any. *I can imagine a
"security" system which would prohibit access to some files, but
for security reasons, refuse to tell you why. *(ie: there is no
way to distinguish between "the file doesn't exist" and "the file
does exist, but you didn't give the correct encryption key".)
I'm sorry, but EACCES is the logical return here, and not telling me
why serves no purpose.
However, if I were on the committee, I would have suggested making
something like a generic EUNKNOWN error code, which could be used
on systems where a failure could occur, but for which the C runtime
could not determine the "true" cause. *(Perhaps the O/S simply
returns a "failed" condition?) *At least, you could then trust that
errno was set, even if you can't guarantee that it was set to a
"known" error code. *For all I know, the suggestion was made, but
reject for some reason.
I'm not buying it. It was a lazy and stupid decision.
Jan 29 '08 #4
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote:
Kaz Kylheku <kk******@gmail.comwrote:
Can you describe a situation in which a C implementor would duck out
of setting errno in fopen, just because the standard allows it?
For instance, suppose that your compiler only emits the message ``this
translation unit requires a diagnostic''. No filename, no line number,
no information.

And yet the standard *does* require a diagnostic, even if it's a
useless one. Why doesn't it do the same for fopen(), requiring
errno to be set, even if to a useless value?
Because it adds nothing of importance to that fopen() call. If all you
want to know is _whether_ the call succeeded, and you either don't care
or aren't allowed to know _why_, you don't need errno, you can just
check whether fopen() returned a null pointer.
Granted, this ignores the scenario where you string several function
calls together, one of which is an fopen(), and you want to check
afterwards if any of them set errno. That is a reason to make fopen()
set errno, but I posit that, in the case of fopen(), it is not as likely
a scenario as in the case of, e.g., a string of floating point
computations.

Richard
Jan 30 '08 #5
user923005 wrote:
[...]
I'm not buying it. It was a lazy and stupid decision.
While I agree that a failing fopen() (and fread() and
getenv() and time() and ...) *ought* to set errno, I see
no evidence that the omission of such a requirement was due
either to laziness or to stupidity.

If you have direct evidence that any of the Standard
writers were lazy or that any were stupid, please reveal it.
Otherwise, lay off the name-calling.

--
Er*********@sun.com
Jan 30 '08 #6
CBFalconer wrote:
[...]
Reminds me of the elegant Microsoft error returns from their
interface software. A routine with about umpty-ump parameters
always returned a code (which had to be laboriously looked up)
which translated to "parameter error". It didn't even identify the
parameter. Untangling that took about two days.
And let's not forget the XP SP2 upgrade, which has a tendency to
give, 25 minutes into the install, the message "Access Denied", and
then spend another 25 minutes undoing what it had done up to that
point. (Yes, that's the entire text of the error message.)

However, your example does relate back to the original "why doesn't
the Standard require fopen() to set errno" question. If the
Standard were to require errno to be set, wouldn't it also need to
define all of the possible error values? Would it make sense for
the Standard to say "errno must be set to an implementation-defined
value"?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Jan 30 '08 #7
On Jan 30, 7:44*am, Eric Sosman <Eric.Sos...@sun.comwrote:
user923005 wrote:
[...]
I'm not buying it. *It was a lazy and stupid decision.

* * *While I agree that a failing fopen() (and fread() and
getenv() and time() and ...) *ought* to set errno, I see
no evidence that the omission of such a requirement was due
either to laziness or to stupidity.

* * *If you have direct evidence that any of the Standard
writers were lazy or that any were stupid, please reveal it.
Otherwise, lay off the name-calling.
I did not say that the standard writers were lazy or stupid. I said
that the decision to not have a failed fopen() set errno was lazy and
stupid. There is a difference between lazy and stupid people and lazy
and stupid decisions (though lazy and stupid people are far more prone
to lazy and stupid decisions than energetic and intelligent ones). My
direct evidence that a lazy and stupid decision was made is that there
is no requirement in the standard for fopen() to set errno.
Jan 30 '08 #8
On Tue, 29 Jan 2008 16:52:29 -0500, Kenneth Brody
<ke******@spamcop.netwrote:
user923005 wrote:
[...]
There are not any compilers that I know of that do not set the errno
value on fopen() failure.
But the fact that you are allowed to leave errno unchanged is clearly
a gaffe.

The fact that neither you nor I know of any systems which do not
set errno does not mean that there aren't any. I can imagine a
"security" system which would prohibit access to some files, but
for security reasons, refuse to tell you why. (ie: there is no
way to distinguish between "the file doesn't exist" and "the file
does exist, but you didn't give the correct encryption key".)
But you can still 'return' an errorcode saying that. Almost exactly
that was done by Multics, one of the biggest completely carried
through efforts at a secure multiuser system. (IIRC it was the only
system at the time to be certified 'orange book' A-something, and one
of the few ever.) I got all too familiar with an errorcode that
decoded as 'insufficient access to return any information', sometimes
because of incorrect ACL settings and sometimes because I had
accidentally requested the wrong thing.
However, if I were on the committee, I would have suggested making
something like a generic EUNKNOWN error code, which could be used
on systems where a failure could occur, but for which the C runtime
could not determine the "true" cause. (Perhaps the O/S simply
returns a "failed" condition?) At least, you could then trust that
errno was set, even if you can't guarantee that it was set to a
"known" error code. For all I know, the suggestion was made, but
reject for some reason.
- formerly david.thompson1 || achar(64) || worldnet.att.net
Feb 10 '08 #9

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

Similar topics

1
by: Martin Lucas-Smith | last post by:
I wrote the function below as part of a larger class. The fopen stage works, and, as according to the documentation at www.php.net/fopen that succesfully creates a new file. The fwrite stage...
1
by: clusardi2k | last post by:
Hello, I have a shared drive on SGI, Linux, and Windows. The fact that I'm using a shared drive may be mute information. The problem is within the same program a second call to fopen does not...
9
by: monomaniac21 | last post by:
Hi everyone i'm trying to setup my website to create new webpages dynamically using php but I am have a major problem in that whenever i create a file it is always created locked so that it can...
11
by: typingcat | last post by:
Is it possible to read another web page in PHP? If is ASP.NET, the code would be ------------ WebRequest req=WebRequest.Create("http://www.microsoft.com"); WebResponse res=req.GetResponse();...
13
by: Blue | last post by:
Hi , Can any one please let me explain me the diffrences between "open"/ "fopen" or "read"/"fread" or "write/fwrite". I know that "open" /"read" / "write" are system calls and "fopen"...
10
by: Longfellow | last post by:
Newbie here... My reading of the description of fopen() led me to expect that, with mode as "w", it would create a file if it did not exist. Checked the FAQ and did not see this question...
39
by: ferrad | last post by:
I am trying to open a file for appending. ie. if the file exists, open it for appending, if not, create it. I am getting back a NULL pointer, and do not know why. Here is my code: FILE...
2
by: doctorhardik | last post by:
hai all when i try create new file using fopen function in php in "W" mode. it dose not create file. Create test.php file in /var/www/html/ directory on linux machine. past this simple code:...
20
by: cscorley | last post by:
For some reason, I cannot use fopen() on the file in write mode. The file "time" is in the same directory as the .php file, with permissions set to 0766. PHP Version 5.2.5 Apache/2.2.8 code...
11
AutumnsDecay
by: AutumnsDecay | last post by:
Hey there. I've been implimenting FCKEditor into my clients websites so they can update the text and pictures on their own. The way it is currently setup is that you need to 'login' to gain...
0
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...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
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...

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.