How to write to a file including full directory in C under Unix?

H

Hongyu

Dear all:

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");
if (fp == NULL)
{
printf("Failed to open file %s\n", fname);
}
else
{
fprintf(fp, "This is just a test only");
}


fclose(fp);


return 0;



}


I also try to write filename and directory with ,".\outdir
\mytestout.txt", or ,".//outdir//mytestout.txt" or ",".\\outdir\
\mytestout.txt", or even ,".///outdir///mytestout.txt" etc, and I
also
tried to specify the whole directory with the current directory .
(dot) replaced by the real directory name, but all failed. I searched
on the internet and only found one relevant that said to specify
filename with full path, but how to specify full path in C under
Unix?
Please help me.

Thanks for the help in advance.


Hongyu
 
C

Chris McDonald

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)


Does the directory ./outdir already exist?
If not, you'll need to create the directory first - use mkdir();

Prepare to be teleported to comp.sys.linux.programmer
 
V

vippstar

Dear all:

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:

Then you want comp.unix.programmer, not comp.lang.c (though your code
is topical)
#include <stdio.h>

int main(void)
{
FILE *fp;
char fname[30];
char pathname[30];

strcpy(fname,"./outdir/mytestout.txt");
fp=fopen(fname, "w");
if (fp == NULL)
{
printf("Failed to open file %s\n", fname);
}
else
{
fprintf(fp, "This is just a test only");
}

fclose(fp);

The fclose call should be in the else clause. fclose(NULL) is not
defined by ISO C. (not to be confused with free(NULL) which _is_
defined and does nothing)
 
A

Antoninus Twink

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:

Guesses: does outdir exist? Are the permissions set correctly for you to
be able to write the file you want to write?

To find out without guessing...
printf("Failed to open file %s\n", fname);

....change this line to
perror("fopen");

and see what error message you get.
 
S

s0suk3

Dear all:

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");

You can just use an initializer:

char fname[] = "./outdir/mytestout.txt";
   fp=fopen(fname, "w");
   if (fp == NULL)
   {
      printf("Failed to open file %s\n", fname);
   }
   else
   {
      fprintf(fp, "This is just a test only");
   }

   fclose(fp);

   return 0;

}

I also try to write filename and directory with ,".\outdir
\mytestout.txt", or ,".//outdir//mytestout.txt" or ",".\\outdir\
\mytestout.txt", or even ,".///outdir///mytestout.txt" etc, and I
also

If your on UNIX, the path separator is '/', so I don't think the path
is the problem.
tried to specify the whole directory with the current directory .
(dot) replaced by the real directory name, but all failed. I searched
on the internet and only found one relevant that said to specify
filename with full path, but how to specify full path in C under
Unix?
Please help me.

Thanks for the help in advance.

Why don't you check the errno variable?


#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>

int main(void)
{
char fname[] = "./outdir/mytestout.txt";

FILE *fp = fopen(fname, "w");
if (fp == NULL)
{
error(0, errno, "could not open %s", fname);
exit(EXIT_FAILURE);
}

fprintf(fp, "This is just a test only");
fclose(fp);

return 0;
}

Sebastian
 
J

Jens Thoms Toerring

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");
if (fp == NULL)
{
printf("Failed to open file %s\n", fname);
}

There could be lots of reasons why you're not able to open that
file in write mode. Just the most likely:

a) It's rather dubious to specify a directory that's relative to
your current path since that restricts your program to be run
from within a single directtory
b) "./outdir" isn't an existing directory
c) "./outdir" isn't a directory you're allowed to change files in
d) "./outdir/mytestout.txt" is a file you're not allowed to open
in write mode.

The simplest way to find out what's really the problem is to call
perror() after fopen() failed. Its output should give you at least
a hint about what went wrong.

Regards, Jens
 
F

Flash Gordon

Hongyu wrote, On 11/08/08 23:51:
Dear all:

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 <errno.h>

strcpy(fname,"./outdir/mytestout.txt");

errno = 0; /* C library functions don't clear errno */
fp=fopen(fname, "w");
if (fp == NULL)
{

if (errno)
perror("The C library reported");

The C standard does not guarantee that errno will be set up fopen, but
it does allow it and many implementations will set it to a useful value.
printf("Failed to open file %s\n", fname);
}
else
{
fprintf(fp, "This is just a test only");
}


fclose(fp);

Here you try and close the file even if you did not succeed in opening
it. This is a *bad* thing to try and could make your program go
***BANG!!!***
return 0;

}


I also try to write filename and directory with ,".\outdir
\mytestout.txt", or ,".//outdir//mytestout.txt" or ",".\\outdir\
\mytestout.txt", or even ,".///outdir///mytestout.txt" etc, and I
also

All of the above are likely to be wrong as you are using Unix.
tried to specify the whole directory with the current directory .
(dot) replaced by the real directory name, but all failed. I searched
on the internet and only found one relevant that said to specify
filename with full path, but how to specify full path in C under
Unix?

comp.unix.programmer would be the place to ask about Unix problems, but
I suspect if you try my C suggestion above you will stand a better
chance of finding the problem.

Oh, and you should probably check the directory exists (including
correct case) and for correct permissions on directories and files, but
that is all Unix specific so ask about it on comp.unix.programmer.
 
H

Hongyu

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");
   if (fp == NULL)
   {
      printf("Failed to open file %s\n", fname);
   }

There could be lots of reasons why you're not able to open that
file in write mode. Just the most likely:

a) It's rather dubious to specify a directory that's relative to
   your current path since that restricts your program to be run
   from within a single directtory
b) "./outdir" isn't an existing directory
c) "./outdir" isn't a directory you're allowed to change files in
d) "./outdir/mytestout.txt" is a file you're not allowed to open
   in write mode.

The simplest way to find out what's really the problem is to call
perror() after fopen() failed. Its output should give you at least
a hint about what went wrong.

                             Regards, Jens
--
  \   Jens Thoms Toerring  ___      (e-mail address removed)
   \__________________________      http://toerring.de- Hide quoted text -

- Show quoted text -


Dear all:

Thanks for all the suggestions. Yes, the "./outdir" isn't an existing
directory, because I want to create that directory and the filename
whenever I want to create it, because it will be used in the situation
when a user typed in the directory name, and i will append the
filename to that directory according to this format. But that was
unsuccessful, so i just tried this simple code to see whether it can
pass. I will test the code Sebastian suggested to see whether it can
pass. If not, what can i do to allow create a directory? I.e.,still
the same as to create a filename with full directory pathname.

Hongyu
 
C

CBFalconer

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 '='.
 
H

Hongyu

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");
   if (fp == NULL)
   {
      printf("Failed to open file %s\n", fname);
   }
There could be lots of reasons why you're not able to open that
file in write mode. Just the most likely:
a) It's rather dubious to specify a directory that's relative to
   your current path since that restricts your program to be run
   from within a single directtory
b) "./outdir" isn't an existing directory
c) "./outdir" isn't a directory you're allowed to change files in
d) "./outdir/mytestout.txt" is a file you're not allowed to open
   in write mode.
The simplest way to find out what's really the problem is to call
perror() after fopen() failed. Its output should give you at least
a hint about what went wrong.
                             Regards, Jens
- Show quoted text -

Dear all:

Thanks for all the suggestions. Yes, the "./outdir" isn't an existing
directory, because I want to create that directory and the filename
whenever I want to create it, because it will be used in the situation
when a user typed in the directory name, and i will append the
filename to that directory according to this format. But that was
unsuccessful, so i just tried this simple code to see whether it can
pass. I will test the code Sebastian suggested to see whether it can
pass. If not, what can i do to allow create a directory? I.e.,still
the same as to create a filename with full directory pathname.

Hongyu- Hide quoted text -

- Show quoted text -

Hi,
I just tried Sebastian's code with adding a line to printf the errno,
and I got:

./a.out: could not open ./outdir/mytestout.txt: No such file or
directory
errno is 2

I then changed the fopen line as FILE *fp = fopen(fname, "w+");
(replaced w+ to w)

but still get the same error. What should I do? Thanks a lot.
 
D

Default User

Hongyu wrote:

I just tried Sebastian's code with adding a line to printf the errno,
and I got:

./a.out: could not open ./outdir/mytestout.txt: No such file or
directory
errno is 2

I then changed the fopen line as FILE *fp = fopen(fname, "w+");
(replaced w+ to w)

but still get the same error. What should I do? Thanks a lot.

You should really be posting to a UNIX newsgroup. However, if the
directory doesn't exist, you can NOT create it by opening a file in it.
No matter how much you want to.

You'll have to first create the directory. Something like:

system("mkdir outdir");

perhaps.



Brian
 
R

Richard Tobin

Hongyu said:
Thanks for all the suggestions. Yes, the "./outdir" isn't an existing
directory, because I want to create that directory and the filename
whenever I want to create it, because it will be used in the situation
when a user typed in the directory name, and i will append the
filename to that directory according to this format.

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.

-- Richard
 
H

Hongyu

You should really be posting to a UNIX newsgroup. However, if the
directory doesn't exist, you can NOT create it by opening a file in it.
No matter how much you want to.

You'll have to first create the directory. Something like:

system("mkdir outdir");

perhaps.

Brian

Thanks. I will try that.
 
H

Hongyu

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.

-- Richard

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.
 
H

Hongyu

Hongyu wrote, On 11/08/08 23:51:



#include <errno.h>



      errno = 0; /* C library functions don't clear errno */


         if (errno)
            perror("The C library reported");

The C standard does not guarantee that errno will be set up fopen, but
it does allow it and many implementations will set it to a useful value.



Here you try and close the file even if you did not succeed in opening
it. This is a *bad* thing to try and could make your program go
***BANG!!!***


All of the above are likely to be wrong as you are using Unix.


comp.unix.programmer would be the place to ask about Unix problems, but
I suspect if you try my C suggestion above you will stand a better
chance of finding the problem.

Oh, and you should probably check the directory exists (including
correct case) and for correct permissions on directories and files, but
that is all Unix specific so ask about it on comp.unix.programmer.

Thanks for the comments, Flash. I do learn a lot from all the replies
on my post. I can see I have a lot to improve even just from this very
short program. But considering that i am a newbie, so it is
reasonable. Yes, you are right, the following question is to check the
status of the directory before i create it. I will ask it in the
comp.unix.programmer and hope they know how to do that in C.
 
H

Hongyu

You should really be posting to a UNIX newsgroup. However, if the
directory doesn't exist, you can NOT create it by opening a file in it.
No matter how much you want to.

You'll have to first create the directory. Something like:

system("mkdir outdir");

perhaps.

Brian

Just like to let you, the people here and the other people who have
similar problems know that your method worked. i.e., a directory must
be created first. But now i have questions on how to check the status
of the directory in C programming under Unix. i.e. whether it is exist
or not, whether it is with writting permission. I am asking this in
comp.unix.programmer now.
 
H

Hongyu

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.
 
B

Bill Reid

Thanks for the comments, Flash. I do learn a lot from all the replies
on my post. I can see I have a lot to improve even just from this very
short program. But considering that i am a newbie, so it is
reasonable. Yes, you are right, the following question is to check the
status of the directory before i create it. I will ask it in the
comp.unix.programmer and hope they know how to do that in C.

You've been trolled by some of the masters of the art here.

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.

If you want to find out if you can open a file, try the POSIX
"C" function access(). Check the documentation for your "C"
compiler for this function, as well mkdir() and and stat()/fstat()
and some other related "POSIX" functions.

For example, to check the basic access to a file for certain
compilers for Windows systems:

/* checks if a specific file exists */
unsigned file_exists(char *file_path) {

#ifdef __WIN32__
if(access(file_path,00)!=SUCCESS)
return FALSE;

/* other system ifdef's elided, including a "Unix" access() */
....
/* if all else fails, this is the C "standard" way of doing it */
#else
FILE *file;

if((file=fopen(file_path,"r"))==NULL)
return FALSE;

fclose(file);
#endif

return TRUE;
}

Note that the C "standard" doesn't recognize the concept of
directories, which is the excuse the trolls will use for their trollery.
However, in most cases you can do any of this stuff using "C",
either through system-specific functions, or "open" POSIX-style
functions.

As far as your specific problem is concerned, it is generally dead-easy
to open a file in a running program in a UNIX filesystem if you have
permissions,
there is nothing very "tricky" about how you specify a path in UNIX (or
"POSIX"; access() will reveal if permissions are the problem, or something
else, via errno. As has been noted, you may have to recursively create
directories if they don't actually exist; I have previously posted code to
do this, but was trolled mercilessly for the information so you're on
your own from here on out...
 
K

Keith Thompson

Hongyu 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 '='.

Can you explain a little bit more on this? I am not sure how to use
blanks around "=" for this? Thanks.

Chuck was merely making a suggestion about the layout of your code,
something that will make it easier to read but will have no effect on
its behavior. You should also add a space after each comma.

Rather than this:

strcpy(fname,"./outdir/mytestout.txt");
fp=fopen(fname, "w");

write this:

strcpy(fname, "./outdir/mytestout.txt");
fp = fopen(fname, "w");
 
K

Keith Thompson

Default User said:
You should really be posting to a UNIX newsgroup. However, if the
directory doesn't exist, you can NOT create it by opening a file in it.
No matter how much you want to.

How do you know that? A conforming C implementation could easily
create any necessary directories as a side effect of creating a file.

You're giving him Unix advice, not C advice. (Your Unix advice
happens to be correct.)
You'll have to first create the directory. Something like:

system("mkdir outdir");

perhaps.

Something like that, yes. But if the above system() call actually
works, then there will very likely be a better, but (equally)
non-portable, way to do the same thing.

<OT>
If system("mkdir outdir") works, then you can replace it with
mkdir("outdir", blah), where "blah" is the second argument to the
mkdir function.

The only good reason I can think of to use system() here is if you
need to check *at run time* whether mkdir is supported on the current
system.
</OT>
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top