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

标签 c

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

相关文章:

c - 如何获取子进程的退出状态?

c - 从C中的文件中读取特定字段

c - TBS6905 DVB-S2 四路调谐器 PCIe 卡的 MAC 地址

c++ - 无法在给定的 xml 文件 libxml2 中正确添加子节点

c - 我该如何正确

c - 如何让 GCC 在没有 "file was built for unsupported file format"的情况下正确编译目标文件?

收集 BST 的所有叶子并列出它们

c - 从队列中删除头元素

c - ANSI C - 数字矩阵的输入

c - 是否有 linux (gcc) 上使用的所有用户数据结构的引用