亲爱的: 我正在尝试写一个文件,其中包含完整的目录名和文件名 指定(./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
推荐答案Hongyu< ho ******* @ yahoowrites: Hongyu <ho*******@yahoowrites: > I我正在尝试写入一个文件,其中包含指定的完整目录名和文件名(./outdir/mytestout.txt,其中。是当前目录) >I am trying to write to a file with full directory name and file namespecified (./outdir/mytestout.txt where . is the current directory)
目录./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.
On 8月12日,凌晨1点51分,Hongyu< hongyu ... @ yahoowrote: On Aug 12, 1:51 am, Hongyu <hongyu...@yahoowrote: 亲爱的: 我正在尝试写一个文件,其中包含完整的目录名和文件名 指定(./outdir/mytestout.txt其中。是当前目录) in C编程语言和Unix下,但是出现了错误的错误 打开文件./outdir/mytestout 。文本。下面是代码: 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:
然后你想要comp.unix.programmer,而不是comp.lang.c(虽然你的代码 是topical )
Then you want comp.unix.programmer, not comp.lang.c (though your code is topical)
#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); #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);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)
> 返回0; } > return 0; }
2008年8月11日22:51,Hongyu写道: On 11 Aug 2008 at 22:51, Hongyu wrote: 我正在尝试写入一个完整目录名和文件名 指定(./outdir/mytestout.txt,其中。是当前目录) C编程语言并在Unix下,但是出现了错误的错误 打开文件./outdir/mytestout.txt。下面是代码: 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:
猜猜: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...
printf(无法打开文件%s \ n,fname); printf("Failed to open file %s\n", fname);....将此行更改为 perror(" fopen"); 并查看您收到的错误消息。
....change this line to perror("fopen"); and see what error message you get.
更多推荐
如何在Unix下写入包含C语句的完整目录的文件?
发布评论