创建目录并将输出文件保存在该目录中
Create directory and save output files in that directory
我想创建一个变量名类似于 "folder Iteration Number %d, Iteration"
的目录,然后将文本输出保存在该文件夹中。
这是我的代码,程序正确创建了目录但没有将文件保存在该目录中,最后一行出现错误。
我试过了
fp1 = fopen("D:\Courses\filename1.plt", "w");
最后一行有效,但我想在我创建的特定文件夹中写入文件。
char directionname[120];
sprintf(directionname, "Profile Iteration Number_%d", it);
mkdir(directionname);
char filename1[120];
sprintf(filename1, "Velocity Profile Iteration_%d.plt", it);
FILE * fp1;
fp1 = fopen("D:\Courses\directionname\filename1.plt", "w");
替换这个
fp1 = fopen("D:\Courses\directionname\filename1.plt", "w");
来自
char fullname[240];
sprintf(fullname, "D:\Courses\%s\%s", directionname, filename1);
fp1 = fopen(fullname, "w");
您没有使用您创建的directionname
。
我猜你想要这样的东西:
char directionname[120];
sprintf(directionname, "Profile Iteration Number_%d", it);
mkdir(directionname);
char filename1[120];
sprintf(filename1, "Velocity Profile Iteration_%d.plt", it);
char filepath[120];
sprintf(filepath, "D:\Courses\%s\%s", directionname, filename1);
FILE * fp1;
fp1 = fopen(filepath, "w");
if (!fp1)
perror(filepath);
fp1 = fopen("D:\Courses\directionname\filename1.plt", "w");
从上面看来,您希望 directionname
和 filename1
被具有这些名称的变量替换。这不是字符串的工作方式。
创建目录时大部分内容都是正确的,但是当您 运行 程序时似乎没有在正确的位置,因此它会在您的当前目录中创建新目录不在 "D:\Courses\" 之下。因此,您应该更改 directionname
以包含您希望新目录所在位置的完整路径。
char directionname[120];
sprintf(directionname, "D:\Courses\Profile Iteration Number_%d", it);
mkdir(directionname);
然后你想像这样在文件名前加上那个值
char filename1[120];
sprintf(filename1, "%s\Velocity Profile Iteration_%d.plt", directionname, it);
filename1
现在应该包含类似 "D:\Courses\Profile Iteration Number_1\Velocity Profile Iteration_1.plt" 的内容,这样您就可以打开它...
FILE * fp1;
fp1 = fopen(filename1, "w");
我想创建一个变量名类似于 "folder Iteration Number %d, Iteration"
的目录,然后将文本输出保存在该文件夹中。
这是我的代码,程序正确创建了目录但没有将文件保存在该目录中,最后一行出现错误。
我试过了
fp1 = fopen("D:\Courses\filename1.plt", "w");
最后一行有效,但我想在我创建的特定文件夹中写入文件。
char directionname[120];
sprintf(directionname, "Profile Iteration Number_%d", it);
mkdir(directionname);
char filename1[120];
sprintf(filename1, "Velocity Profile Iteration_%d.plt", it);
FILE * fp1;
fp1 = fopen("D:\Courses\directionname\filename1.plt", "w");
替换这个
fp1 = fopen("D:\Courses\directionname\filename1.plt", "w");
来自
char fullname[240];
sprintf(fullname, "D:\Courses\%s\%s", directionname, filename1);
fp1 = fopen(fullname, "w");
您没有使用您创建的directionname
。
我猜你想要这样的东西:
char directionname[120];
sprintf(directionname, "Profile Iteration Number_%d", it);
mkdir(directionname);
char filename1[120];
sprintf(filename1, "Velocity Profile Iteration_%d.plt", it);
char filepath[120];
sprintf(filepath, "D:\Courses\%s\%s", directionname, filename1);
FILE * fp1;
fp1 = fopen(filepath, "w");
if (!fp1)
perror(filepath);
fp1 = fopen("D:\Courses\directionname\filename1.plt", "w");
从上面看来,您希望 directionname
和 filename1
被具有这些名称的变量替换。这不是字符串的工作方式。
创建目录时大部分内容都是正确的,但是当您 运行 程序时似乎没有在正确的位置,因此它会在您的当前目录中创建新目录不在 "D:\Courses\" 之下。因此,您应该更改 directionname
以包含您希望新目录所在位置的完整路径。
char directionname[120];
sprintf(directionname, "D:\Courses\Profile Iteration Number_%d", it);
mkdir(directionname);
然后你想像这样在文件名前加上那个值
char filename1[120];
sprintf(filename1, "%s\Velocity Profile Iteration_%d.plt", directionname, it);
filename1
现在应该包含类似 "D:\Courses\Profile Iteration Number_1\Velocity Profile Iteration_1.plt" 的内容,这样您就可以打开它...
FILE * fp1;
fp1 = fopen(filename1, "w");