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

时间:2019-01-27 22:24:20

标签: 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();
    }

1 个答案:

答案 0 :(得分:0)

这里有一个功能代码:

#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;
}