如何写入包含C中目录名的文件? [英] How to write to a file including directory name in C?

查看:48
本文介绍了如何写入包含C中目录名的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的:


我正在尝试写一个文件,其中包含完整的目录名和文件名

指定(./outdir/mytestout.txt其中。是当前目录)

C编程语言并且在Unix下,但是出现了错误,无法打开
打开文件./outdir/mytestout.txt。以下是代码:


#include< stdio.h>


int main(无效)

{

FILE * fp;

char fname [30];

char pathname [30];


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

fp = fopen(fname," w");

if(fp == NULL)

{

printf(无法打开文件%s \ n,fname);

}

其他

{

fprintf(fp,这只是一个测试);

}


fclose;


返回0;


}


我也尝试使用.\outdir

\ mytestout.txt或.// outdir // mytestout.txt来编写文件名和目录。或,,。\\outdir \

\ mytestout.txt",甚至,。/ / outdir /// mytestout.txt等等,我也用
尝试用当前目录指定整个目录。

(点)替换为真实目录名,但都失败了。我在互联网上搜索了

,发现只有一个相关的说明用完整路径指定

文件名,但如何在Unix下用C指定完整路径?

请帮帮我。


感谢您的帮助。


Hongyu

解决方案

8月11日,5:26 * pm,Hongyu< hongyu ... @ yahoo.comwrote:


亲爱的all:


我正在尝试写入一个文件,其中包含完整的目录名和文件名

指定(./outdir/mytestout.txt,其中。是目前的目录)

C编程语言和Unix下的,但是出现了错误的错误

打开文件./outdir/mytestout.txt。以下是代码:


#include< stdio.h>


int main(无效)

{

* * FILE * fp;

* * char fname [30];

* * char pathname [30];


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

* * fp = fopen(fname," w");

* * if(fp == NULL)

* * {

* * * printf("无法打开文件%s \ n",fname);

* *}

* *其他

* * {

* * * fprintf(fp,这只是一个测试);

* *}


* * fclose;


* *返回0;


}


我也尝试使用.\outdir编写文件名和目录

\ mytestout.txt",或者,.// outdir // mytestout.txt或,,。\\outdir \

\ mytestout.txt",甚至,。/ / outdir /// mytestout.txt等等,我也用
尝试用当前目录指定整个目录。

(点)替换为真实目录名,但都失败了。我在互联网上搜索了

,发现只有一个相关的说明用完整路径指定

文件名,但如何在Unix下用C指定完整路径?

请帮帮我。


感谢您的帮助。


Hongyu



还有一个信息。实际上,我在fclose行中的代码已被修改为fclose(fp),但我仍然收到错误无法打开

文件./outdir/ mytestout.txt

分段错误。


Hongyu写道:


我正在尝试写入一个文件,其中包含完整的目录名和文件名

指定(./outdir/mytestout.txt,其中。是当前目录)

C编程语言,在Unix下,[..]



尝试新闻:comp.lang.c或众多Unix新闻组中的任何一个。一般来说,应该避免

相对路径。


V

-

请删除资金''A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问


8月11日下午5:48 *,Victor Bazarov< v.Abaza ... @ comAcast.netwrote:


Hongyu写道:
< blockquote class =post_quotes>
我正在尝试写一个文件,其中包含完整的目录名和文件名

指定(./outdir/mytestout.txt,其中。是当前目录)在

C编程语言和Unix下,[..]



尝试新闻:comp.lang.c或任何众多的Unix新闻组。 *一般来说,

相对路径应该避免。


V

-

请删除资本''A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问



谢谢。我会试试。


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;

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

解决方案

On Aug 11, 5:26*pm, Hongyu <hongyu...@yahoo.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");
* *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;

* *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

One more information. Actually, my code in the line of fclose has been
corrected as fclose(fp), but I still got error of "Failed to open
file ./outdir/mytestout.txt
Segmentation fault."


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, [..]

Try news:comp.lang.c or any of the numerous Unix newsgroups. Generally,
relative paths should be avoided.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Aug 11, 5:48*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:

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, [..]


Try news:comp.lang.c or any of the numerous Unix newsgroups. *Generally,
relative paths should be avoided.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask

Thanks. I will try that.


这篇关于如何写入包含C中目录名的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆