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

问题描述

亲爱的:


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

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

in

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(fp);

返回0;


}

我也尝试用.\outdir

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

\ mytestout.txt",甚至,。/ / outdir /// mytestout.txt等等,我还是
还是

试图用当前目录指定整个目录。

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

,只找到一个相关的说明用完整路径指定

文件名,但是如何在
$ b下指定C中的完整路径$ b Unix?

请帮帮我。


感谢您的帮助。

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

推荐答案



目录./outdir是否已经存在?

如果没有,你需要先创建目录 - 使用mkdir();


准备被传送到comp.sys.linux.programmer


-

Chris。


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.




然后你想要comp.unix.programmer,而不是comp.lang.c(虽然你的代码

是topical )

Then you want comp.unix.programmer, not comp.lang.c (though your code
is topical)



fclose调用应该在else子句中。 fclose(NULL)不是由ISO C定义的
。(不要与free(NULL)混淆,_is_

定义并且什么都不做)

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)




猜猜:outdir存在吗?是否正确设置了权限

能够写出你想要写的文件吗?


无需猜测即可找到...

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



....将此行更改为

perror(" fopen");


并查看您收到的错误消息。

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

and see what error message you get.


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

09-13 22:20