473,847 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 ",".\\outdi r\
\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
Aug 11 '08
65 5115
>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.
Pick your approach:

1. Specify a path using an existing directory.
2. Manually create the directory first.
3. Parse the file name for parent directories, and check if they
exist, and create them if not. This likely requires functions
that are not standard C, like mkdir(), stat(), and the assumption
that '/' is a path separator.

Aug 11 '08 #11
On Aug 11, 7:49*pm, "Default User" <defaultuse...@ yahoo.comwrote:
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
Thanks. I will try that.
Aug 12 '08 #12
On Aug 11, 7:54*pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <18a29ac5-cd6f-4da9-8ae0-73e7127db...@2g 2000hsn.googleg roups.com>,

Hongyu *<hongyu...@yah oo.comwrote:
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
--
Please remember to mention me / in tapes you leave behind.
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.
Aug 12 '08 #13
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>

<snip>
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 ",".\\outdi r\
\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.progr ammer 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.progr ammer.
--
Flash Gordon
Aug 12 '08 #14
On Aug 11, 7:22*pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
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>

<snip>
* *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 ",".\\outdi r\
\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.progr ammer 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.progr ammer.
--
Flash Gordon
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.progr ammer and hope they know how to do that in C.
Aug 12 '08 #15
Hongyu wrote:
>
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.

Aug 12 '08 #16
On Aug 11, 7:49*pm, "Default User" <defaultuse...@ yahoo.comwrote:
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
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.progr ammer now.
Aug 12 '08 #17
On Aug 11, 7:30*pm, CBFalconer <cbfalco...@yah oo.comwrote:
Hongyu wrote:
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.
Aug 12 '08 #18

Hongyu <ho*******@yaho o.comwrote in message
news:38******** *************** ***********@f36 g2000hsa.google groups.com...
On Aug 11, 7:22 pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
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.progr ammer 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.progr ammer.
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.progr ammer 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(cha r *file_path) {

#ifdef __WIN32__
if(access(file_ path,00)!=SUCCE SS)
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...

---
William Ernest Reid
Aug 12 '08 #19
Hongyu <ho*******@yaho o.comwrites:
On Aug 11, 7:30*pm, CBFalconer <cbfalco...@yah oo.comwrote:
>Hongyu wrote:
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");

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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"
Aug 12 '08 #20

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

Similar topics

0
789
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
3
14067
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
12
8549
by: Hongyu | last post by:
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)
0
9732
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
10712
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
10344
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...
0
9487
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7886
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
7058
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
5723
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4536
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4126
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.