473,598 Members | 3,228 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 #1
65 5048
Hongyu <ho*******@yaho o.comwrites:
>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

--
Chris.
Aug 11 '08 #2
On Aug 12, 1:51 am, Hongyu <hongyu...@yaho o.comwrote:
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.progr ammer, 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)
>
return 0;

}
Aug 11 '08 #3
On 11 Aug 2008 at 22:51, 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:
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.

Aug 11 '08 #4
On Aug 11, 5:51*pm, Hongyu <ho*******@yaho o.comwrote:
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 ",".\\outdi r\
\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_FAILU RE);
}

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

return 0;
}

Sebastian
Aug 11 '08 #5
Hongyu <ho*******@yaho o.comwrote:
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 ___ jt@toerring.de
\______________ ____________ http://toerring.de
Aug 11 '08 #6
On Aug 11, 7:13*pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
Hongyu <hongyu...@yaho o.comwrote:
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 *___ * * *j...@toerring. de
* *\_____________ _____________ * * *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
Aug 11 '08 #7
On Aug 11, 7:25*pm, Hongyu <hongyu...@yaho o.comwrote:
On Aug 11, 7:13*pm, j...@toerring.d e (Jens Thoms Toerring) wrote:


Hongyu <hongyu...@yaho o.comwrote:
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 openfile
./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 *___ * * *j...@toerring. de
* *\_____________ _____________ * * *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- 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.

Aug 11 '08 #8
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
Aug 11 '08 #9
In article <18************ *************** *******@2g2000h sn.googlegroups .com>,
Hongyu <ho*******@yaho o.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.
Aug 11 '08 #10

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
14046
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
8530
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
7992
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
8400
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8267
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
6725
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
5850
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
3898
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
2414
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
1
1505
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
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.