我需要有关创建文件夹的程序的帮助,并在该文件夹中创建一个小的 txt 文件。我创建了一个创建文件夹的程序,但我不知道如何将 .txt 文件放入该文件夹中。程序必须相同,它创建文件夹和 txt 文件。这是我的代码和我所做的
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<dir.h>
#define SIZE 25 //Maximum Length of name of folder
void main() {
int check;
char *dirname;
dirname=malloc(SIZE*sizeof(char));
printf("Enter a directory path and name to be created (C:/name):");
gets(dirname);
check = mkdir(dirname);
if (!check)
printf("Directory created\n");
else
{
printf("Unable to create directory\n");
exit(1);
}
getch();
system("dir/p");
getch();
}
这里有一个功能代码:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<dir.h>
#define SIZE 25 //Maximum Length of name of folder
int main()
{
int check;
char *dirname, *filename, *fulldir;
FILE *fp;
dirname=malloc(SIZE*sizeof(char));
filename=malloc(SIZE*sizeof(char));
fulldir=malloc(SIZE*sizeof(char));
printf("Enter a directory path and name to be created (C:/name):");
gets(dirname);
check = mkdir(dirname);
if (!check)
printf("Directory created\n");
else
{
printf("Unable to create directory\n");
exit(1);
}
// Gets filename
printf("Enter a file name (filename.txt):");
gets(filename);
// Create full direction
strcpy(fulldir, dirname);
strcat(fulldir, "/");
strcat(fulldir, filename);
// Create file inside created folder
if (fp = fopen(fulldir, "w") != NULL ) {
printf("File created\n");
// Close openned file
fclose(fp);
} else {
printf("Unable to create file\n");
}
// End function
return 0;
}
我是一名优秀的程序员,十分优秀!