本文介绍了如何写入包含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 解决方案 还有一个信息。实际上,我在fclose行中的代码已被修改为fclose(fp),但我仍然收到错误无法打开 文件./outdir/ mytestout.txt 分段错误。 尝试新闻:comp.lang.c或众多Unix新闻组中的任何一个。一般来说,应该避免 相对路径。 V - 请删除资金''A'在通过电子邮件回复时 我没有回复最热门的回复,请不要问 尝试新闻:comp.lang.c或任何众多的Unix新闻组。 *一般来说, 相对路径应该避免。 V - 请删除资本''A'在通过电子邮件回复时 我没有回复最热门的回复,请不要问 谢谢。我会试试。 Dear all: I am trying to write to a file with full directory name and file namespecified (./outdir/mytestout.txt where . is the current directory) inC programming language and under Unix, but got errors of Failed toopen 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 alsotried to specify the whole directory with the current directory .(dot) replaced by the real directory name, but all failed. I searchedon the internet and only found one relevant that said to specifyfilename 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 beencorrected as fclose(fp), but I still got error of "Failed to openfile ./outdir/mytestout.txtSegmentation fault." 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-mailI do not respond to top-posted replies, please don''t ask 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-mailI do not respond to top-posted replies, please don''t askThanks. I will try that. 这篇关于如何写入包含C中目录名的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-22 17:29