473,651 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5608
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...@gmai l.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.l earn.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...@spamc op.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...@su n.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******@spamc op.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.ne t
Feb 10 '08 #9

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

Similar topics

1
6550
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 returns false, however, on the first time the function is run, but returns true the second time it is run. Can anyone suggest why fwrite only works the second time?
1
2113
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 create a file if the file has been deleted. I would like to use fopen for its pointer return value to solve this.
9
2791
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 only be read even when i specify read and write access. Here is a basic example of the code I am using (I am using a Mac OSX 10.3.9):
11
5791
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(); StreamReader sr = new StreamReader(res.GetResponseStream()); String Output=sr.ReadToEnd(); Response.Write("The content is : " + Output); --------
13
22618
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" /"fread" / "fwrite" are normal library functions. May be internally must be calling "fopen" /"fread" / "fwrite" functions.
10
1924
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 addressed. What don't I understand? Thanks, Longfellow
39
23777
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 *pFile; char *cFileName; cFileName=argv; pFile = fopen(cFileName,"a+"); cFileName has a valid filename ('apm.dbg'), and I have full write
2
5524
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: <?php
20
5101
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 snip in question: $file = "time";
11
2743
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 access to the editor. Once the changes are made, the user will click submit and the editor will write (in HTML) what the user wrote out in rich text. The way that I have the editor writing to the file is like this: I use the PHP fopen command to...
0
8357
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8277
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8581
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.