S
s0suk3
How do you know that? A conforming C implementation could easily
create any necessary directories as a side effect of creating a file.
How?
<snip>
Sebastian
How do you know that? A conforming C implementation could easily
create any necessary directories as a side effect of creating a file.
------------------ snip from "-- " to here. ---------------Hongyu said:CBFalconer said:Hongyu said:I am trying to write to a file with full directory name and file
name specified (./outdir/mytestout.txt where . is the current
directory) in C programming language and under Unix, but got
errors of Failed to open file ./outdir/mytestout.txt. Below is
the code:
#include <stdio.h>
int main(void)
{
FILE *fp;
char fname[30];
char pathname[30];
strcpy(fname,"./outdir/mytestout.txt");
fp=fopen(fname, "w");
Look into permissions. On principal, use blanks around '='.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.- Hide quoted text -
- Show quoted text -
Can you explain a little bit more on this? I am not sure how to use
blanks around "=" for this? Thanks.
How?
Thanks. I know how to create directory in Unix using mkdir, but what i
don't know is how to make it in the C programming. I will try Brian's
method which mentioned using system method to achieve it. I guess it
will work.
[/QUOTE]You need to create the directory before you create the file in it.
On unix, use the mkdir() function - see the manual page for details.
Thanks. I know how to create directory in Unix using mkdir, but what i
don't know is how to make it in the C programming.
On principal, use blanks around '='.
Bill Reid said:What you're really looking for is something called "POSIX",
which was initially developed based on Unix, but could potentially
apply to any computer system, and as a matter of fact is
included in the "C" compilers for many different types of
systems.
On 11 Aug 2008 at 23:57, Hongyu wrote:
It will "work", but it is a stupid solution.
"Brian" is a troll who
wants to disrupt the group with endless topicality and netnanny flame
wars,
and in this case he has led you down the wrong path. Running
programs using system() can be a security issue,
and you get no way of
discovering errors that may occur.
Instead, use the portable POSIX solution:
#include <sys/stat.h>
#include <sys/types.h>
then do something like
if(mkdir("my_new_directory", S_IRWXU | S_IRWXG | S_IRWXO) == -1) {
/* an error occurred - check errno to find out what */} else {
/* success - carry on */
[/QUOTE]It will "work", but it is a stupid solution.
this is probably so, under Posix. On another OS it may be the only
way.
Richard said:Very unlikely - do you know of any operating system where that is
true?. And the system() version is just as unportable as the mkdir()
version, because you have to know what the operating system's command
for creating directories is. If you can find that out, you can find
out what the function is.
> In article <29290ece-45b5-473a-a12e-a64b4494a68d@l64g2000hse.googlegroups.com>, ....
>
> Very unlikely - do you know of any operating system where that is
> true?
> And the system() version is just as unportable as the mkdir()
> version, because you have to know what the operating system's command
> for creating directories is. If you can find that out, you can find
> out what the function is.
In some system-specific manner.
Assuming a Unix-like system, fopen("/foo/bar/newfile.txt", "w") could
implicitly invoke the equivalent of "mkdir -p /foo/bar" before
actually creating the file.
That's just one example. The point is that the C standard doesn't
mention directories at all. There's nothing in the standard that says
an implementation *can't* create a directory as a side effect of
creating a file.
(I'm not aware of any actual implementation that does this.)
--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"- Hide quoted text -
- Show quoted text -
Thanks everyone's help. The discussion really helps, not only just on
my specific question, but also on the good coding styles etc. I think
what you mentioned here is what I tried in the beginning, but
unfortunately it didn't work, unless i create the directory first. I
am als in a Unix-like system (Linux). I think as people suggested
here, "POSIX" functions are way to go. I will study it more and try
it, and let people know the result after the testing.- Hide quoted text -
- Show quoted text -
Thanks everyone's help. The discussion really helps, not only just on
my specific question, but also on the good coding styles etc. I think
what you mentioned here is what I tried in the beginning, but
unfortunately it didn't work, unless i create the directory first. I
am als in a Unix-like system (Linux). I think as people suggested
here, "POSIX" functions are way to go. I will study it more and try
it, and let people know the result after the testing.- Hide quoted text -- Show quoted text -
The discussion is really helpful and interesting. Thanks everyone's
help again. For some unknown reasons, my questioning post was not
appeared in the unix.programmer group discussion board. But from the
suggestions here, I got the code to work. Just to let people with
similar questions know how to solve the problem, here is the code I
used and POISX is the way to go. It should be mentioned that multi-
level subdirectories can only be created in the code recursively or
the parent directory is exist and writtable first (i didn't do
recursive coding in the code here).
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main(void)
{
char fname[40] = "mytestout.txt"; //filename
//char fpath[40] = "./testdir1/testdir2"; // filepath, doesn't work
unless testdir1 is exist and writable
char fpath[40] = "./testdir1"; // filepath, worked!
//char fpath[40] = "."; //filepath. curent directory. worked!
char wholename[100]; //filename with full path
int last;
last = (int)strlen(fpath)-1; // last char of path
//make sure the path is ended with '/'
if ((last >= 0) && (fpath[last] != '/'))
{
last++;
fpath[last] = '/'; // append '\' at the last of path
fpath[last+1] = '\0'; // terminate the path char string
}
strcpy(wholename, fpath);
strcat(wholename, fname);
// create the directory if it is not exist
if(access(fpath, F_OK) != 0) // directory nonexist
{ // actually I don't need to check this
// because the man mkdir says 'create a
directory
// if it is non-exist'. But to make
sure, keep it here
// create the directory with read/write/search permissions for
owner and group,
// and with read/search permissions for others
if(mkdir(fpath,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
{
printf("Failed to create directory %s\n", fpath);
return EXIT_FAILURE;
}
}
// write to file whose name is spcified with full path
FILE *fp = fopen(wholename, "w");
if (fp == NULL)
{
error(0, errno, "could not open %s", fname);
exit(EXIT_FAILURE);
}
else
{
fprintf(fp, "This is just a test!");
fclose(fp);
}
return EXIT_SUCCESS;
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
The discussion is really helpful and interesting. Thanks everyone's
help again. For some unknown reasons, my questioning post was not
appeared in the unix.programmer group discussion board. But from the
suggestions here, I got the code to work. Just to let people with
similar questions know how to solve the problem, here is the code I
used and POISX is the way to go. It should be mentioned that multi-
level subdirectories can only be created in the code recursively or
the parent directory is exist and writtable first (i didn't do
recursive coding in the code here).#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>int main(void)
{
char fname[40] = "mytestout.txt"; //filename
//char fpath[40] = "./testdir1/testdir2"; // filepath, doesn't work
unless testdir1 is exist and writable
char fpath[40] = "./testdir1"; // filepath, worked!
//char fpath[40] = "."; //filepath. curent directory. worked!
char wholename[100]; //filename with full path
int last;last = (int)strlen(fpath)-1; // last char of path//make sure the path is ended with '/'
if ((last >= 0) && (fpath[last] != '/'))
{
last++;
fpath[last] = '/'; // append '\' at the last of path
fpath[last+1] = '\0'; // terminate the path char string
}strcpy(wholename, fpath);strcat(wholename, fname);// create the directory if it is not exist
if(access(fpath, F_OK) != 0) // directory nonexist
{ // actually I don't need to check this
// because the man mkdir says 'create a
directory
// if it is non-exist'. But to make
sure, keep it here// create the directory with read/write/search permissions for
owner and group,
// and with read/search permissions for others
if(mkdir(fpath,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
{
printf("Failed to create directory %s\n", fpath);
return EXIT_FAILURE;
}
}// write to file whose name is spcified with full path
FILE *fp = fopen(wholename, "w");
if (fp == NULL)
{
error(0, errno, "could not open %s", fname);
exit(EXIT_FAILURE);
}
else
{
fprintf(fp, "This is just a test!");
fclose(fp);
}return EXIT_SUCCESS;}- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
Sorry, the code didn't appear 100% right after the posting since the
comment lines get adjusted. Here is after the re-adjusting to make it
right:
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main(void)
{
char fname[40] = "mytestout.txt"; //filename
//char fpath[40] = "./testdir1/testdir2"; // filepath, doesn't work
unless testdir1 is exist and writable
char fpath[40] = "./testdir1"; // filepath, worked!
//char fpath[40] = "."; //filepath. curent directory. worked!
char wholename[100]; //filename with full path
int last;
last = (int)strlen(fpath)-1; // last char of path
//make sure the path is ended with '/'
if ((last >= 0) && (fpath[last] != '/'))
{
last++;
fpath[last] = '/'; // append '\' at the last of path
fpath[last+1] = '\0'; // terminate the path char string
}
strcpy(wholename, fpath);
strcat(wholename, fname);
// create the directory if it is not exist
if(access(fpath, F_OK) != 0) // directory nonexist
{ // actually I don't need to check
this
// because the man mkdir says 'create a
directory
// if it is non-exist'. But to make
sure, keep it here
// create the directory with read/write/search permissions for
owner and group,
// and with read/search permissions for others
if(mkdir(fpath,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
{
printf("Failed to create directory %s\n", fpath);
return EXIT_FAILURE;
}
}
// write to file whose name is spcified with full path
FILE *fp = fopen(wholename, "w");
if (fp == NULL)
{
error(0, errno, "could not open %s", fname);
exit(EXIT_FAILURE);
}
else
{
fprintf(fp, "This is just a test!");
fclose(fp);
}
return EXIT_SUCCESS;
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Hongyu said:Thanks everyone's help. The discussion really helps, not only
just on my specific question, but also on the good coding styles
etc. I think what you mentioned here is what I tried in the
beginning, but unfortunately it didn't work, unless i create the
directory first. I am als in a Unix-like system (Linux). I think
as people suggested here, "POSIX" functions are way to go. I will
study it more and try it, and let people know the result after
the testing.
Unless you have specific reasons for needing to use POSIX
functions,
you should stick to the standard C functions. Most of the time the
standard functions will be adequate, and in some cases the results
will be more efficient.
Using things outside the C standard always reduces your portability.
... For some unknown reasons, my questioning post was not
appeared in the unix.programmer group discussion board.
... snip ...
Unless you have specific reasons for needing to use POSIX
functions, you should stick to the standard C functions. Most of
the time the standard functions will be adequate, and in some cases
the results will be more efficient. Using things outside the C
standard always reduces your portability.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.