gpt4 book ai didi

c - 如何将txt文件放入文件夹C编程

转载 作者:太空宇宙 更新时间:2023-11-04 04:13:34 25 4
gpt4 key购买 nike

我需要有关创建文件夹的程序的帮助,并在该文件夹中创建一个小的 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;
}

关于c - 如何将txt文件放入文件夹C编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54393424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com