C++编程在文件夹中创建文件

create file in a folder in c++ programming

如何在文件夹中创建文件c编程?

我有一个程序,它将获取 ID 号并将其作为文件名,我想将该文件保存到特定文件夹中,但它总是将文件名设为 "id"。 样本:

 #include<stdio.h>
    #include<iostream>
    using namespace std;
    int main()
    {   FILE *filehandling;
    char inputA, name[20], birthday[20], gender[6], add[50], contact[15], id[15];
        cout<<"Name\t\t   : ";
        gets(name);
        cout<<"Birthday mm/dd/yy  : ";
        cin>>birthday;
        cout<<"Gender\t\t   : ";
        cin>>gender;
        cout<<"I.D #\t\t   : ";
        cin>>id;
        cout<<"Contact #\t   : ";
        cin>>contact;
        cout<<"Address\t\t   : ";
        gets(add);

    filehandling = fopen(id,"a");
    fprintf(filehandling, "Name\t   : %s\n", name);
    fprintf(filehandling, "Birthday   : %s\n", birthday);
    fprintf(filehandling, "Gender\t   : %s\n", gender);
    fprintf(filehandling, "I.D #\t   : %s\n", id);
    fprintf(filehandling, "Contact #  : %s\n", contact);
    fprintf(filehandling, "Address\t   : %s\n", add);
    fclose(filehandling);
    }

我没有完全理解你的问题..

但是..你为什么不做一个文件夹??

做一个文件夹,里面写一个文件。

...
char folder_name[ MAX_PATH ];
sprintf( folder_name, "c:\folder\%s", folder_for_id );
mkdir( folder_name );
char filename[ MAX_PATH ];
sprintf( filename, "%s\%s", folder_name, id );
FILE *fp = fopen( filename, "wt" );
...