C 创建一个文件

C 创建一个文件,c,file-io,C,File Io,我试图创建3个不同的文件,其中包括随机记录。创建随机记录没有问题。更重要的是,创建其中两个也没有问题。但无论我做什么,都没有创建文件dataFile2.dat。我真的很困惑。有人知道为什么吗 struct record{ char numbers [11],letters[11],date[11]; }; int main(int argc, char *argv[]) { writeRecordToFile("dataFile1.dat", 1000000); writeR

我试图创建3个不同的文件,其中包括随机记录。创建随机记录没有问题。更重要的是,创建其中两个也没有问题。但无论我做什么,都没有创建文件
dataFile2.dat
。我真的很困惑。有人知道为什么吗

struct record{ char numbers [11],letters[11],date[11]; }; 

int main(int argc, char *argv[])
{
    writeRecordToFile("dataFile1.dat", 1000000);
    writeRecordToFile("dataFile2.dat", 1000000);
    writeRecordToFile("dataFile3.dat", 1000000);
} 



int writeRecordToFile(char*fileName, int numberRecord)
{
    int i;
    for(i=numberRecord; i>0; i--)
    {
        struct record *newRecord=malloc(sizeof(struct record));
        strcpy(newRecord->numbers,randstring(10,1)); 
        strcpy(newRecord->letters,randstring(10,0));
        strcpy(newRecord->date,randomDate());       

        //createRandom Record // changed of global variable's content
       FILE * file= fopen(fileName, "a");
        if (file != NULL) {
            fprintf(file, "%s,%s,%s\n", newRecord->numbers,newRecord->letters,newRecord->date); 
            fclose(file);
        }   
        else{
            puts("Error handled when appending file");
            return -1;
        }
    }   
}
好的,这里是最终版本(实际上这里可能不需要使用struct,而且我首先避免使用动态内存)

好的,这里是最终版本(实际上这里可能不需要使用struct,而且我首先避免使用动态内存)

好的,这里是最终版本(实际上这里可能不需要使用struct,而且我首先避免使用动态内存)

好的,这里是最终版本(实际上这里可能不需要使用struct,而且我首先避免使用动态内存)


你的问题是你一直在循环中调用
fopen
,甚至没有调用
fclose
,而
malloc
没有调用
free
,所以你的资源用完了,这应该行得通

int writeRecordToFile(char*fileName, int numberRecord)
{
    int i;
    /* You only need to open the file once */
    FILE * file= fopen(fileName, "a");
    if (file == NULL) /* check fopen succeeded. */
        return -1;
    /* You only need to call malloc once and reuse memory in the loop */
    struct record *newRecord=malloc(sizeof(struct record));
    if (newRecord == NULL) /* always check malloc return value */
    {
        fclose(file);
        return -1;
    }

    for(i=numberRecord; i>0; i--)
    {
        strcpy(newRecord->numbers,randstring(10,1));
        strcpy(newRecord->letters,randstring(10,0));
        strcpy(newRecord->date,randomDate());

        //createRandom Record // changed of global variable's content
        fprintf(file, "%s,%s,%s\n", newRecord->numbers,newRecord->letters,newRecord->date);
    }
    fclose(file);
    free(newRecord);
}

另外,我不认为使用struct来实现这一点有什么意义,如果
randstring
动态分配内存,那么您仍然存在内存泄漏。

您的问题是,您在循环中一直调用
fopen
,甚至没有调用
fclose
,而且
malloc
没有调用
free
所以您的资源用完了,这应该可以工作

int writeRecordToFile(char*fileName, int numberRecord)
{
    int i;
    /* You only need to open the file once */
    FILE * file= fopen(fileName, "a");
    if (file == NULL) /* check fopen succeeded. */
        return -1;
    /* You only need to call malloc once and reuse memory in the loop */
    struct record *newRecord=malloc(sizeof(struct record));
    if (newRecord == NULL) /* always check malloc return value */
    {
        fclose(file);
        return -1;
    }

    for(i=numberRecord; i>0; i--)
    {
        strcpy(newRecord->numbers,randstring(10,1));
        strcpy(newRecord->letters,randstring(10,0));
        strcpy(newRecord->date,randomDate());

        //createRandom Record // changed of global variable's content
        fprintf(file, "%s,%s,%s\n", newRecord->numbers,newRecord->letters,newRecord->date);
    }
    fclose(file);
    free(newRecord);
}

另外,我不认为使用struct来实现这一点有什么意义,如果
randstring
动态分配内存,那么您仍然存在内存泄漏。

您的问题是,您在循环中一直调用
fopen
,甚至没有调用
fclose
,而且
malloc
没有调用
free
所以您的资源用完了,这应该可以工作

int writeRecordToFile(char*fileName, int numberRecord)
{
    int i;
    /* You only need to open the file once */
    FILE * file= fopen(fileName, "a");
    if (file == NULL) /* check fopen succeeded. */
        return -1;
    /* You only need to call malloc once and reuse memory in the loop */
    struct record *newRecord=malloc(sizeof(struct record));
    if (newRecord == NULL) /* always check malloc return value */
    {
        fclose(file);
        return -1;
    }

    for(i=numberRecord; i>0; i--)
    {
        strcpy(newRecord->numbers,randstring(10,1));
        strcpy(newRecord->letters,randstring(10,0));
        strcpy(newRecord->date,randomDate());

        //createRandom Record // changed of global variable's content
        fprintf(file, "%s,%s,%s\n", newRecord->numbers,newRecord->letters,newRecord->date);
    }
    fclose(file);
    free(newRecord);
}

另外,我不认为使用struct来实现这一点有什么意义,如果
randstring
动态分配内存,那么您仍然存在内存泄漏。

您的问题是,您在循环中一直调用
fopen
,甚至没有调用
fclose
,而且
malloc
没有调用
free
所以您的资源用完了,这应该可以工作

int writeRecordToFile(char*fileName, int numberRecord)
{
    int i;
    /* You only need to open the file once */
    FILE * file= fopen(fileName, "a");
    if (file == NULL) /* check fopen succeeded. */
        return -1;
    /* You only need to call malloc once and reuse memory in the loop */
    struct record *newRecord=malloc(sizeof(struct record));
    if (newRecord == NULL) /* always check malloc return value */
    {
        fclose(file);
        return -1;
    }

    for(i=numberRecord; i>0; i--)
    {
        strcpy(newRecord->numbers,randstring(10,1));
        strcpy(newRecord->letters,randstring(10,0));
        strcpy(newRecord->date,randomDate());

        //createRandom Record // changed of global variable's content
        fprintf(file, "%s,%s,%s\n", newRecord->numbers,newRecord->letters,newRecord->date);
    }
    fclose(file);
    free(newRecord);
}



另外,我不认为使用struct有什么意义,如果
randstring
正在动态分配内存,您仍然存在内存泄漏。

您是否在失败的文件上看到自己的错误消息?@Jongware否,终端上没有消息,好像没有问题。您是否检查了您的第一个文件是否具有预期的记录数?您可能会遇到内存泄漏问题,调用
malloc
1000000次。您没有检查
malloc()
是否成功的原因是什么?另外,请向我们展示
结构记录
结构。编号的数据类型是什么?我希望它不是
char*
@harper是的,我检查了它,也检查了3。文件两个都正常。您在失败的文件上看到您自己的错误消息了吗?@Jongware否,终端上没有消息,好像没有问题。您是否检查了您的第一个文件是否具有预期的记录数?您可能会遇到内存泄漏问题,调用
malloc
1000000次。您没有检查
malloc()
是否成功的原因是什么?另外,请向我们展示
结构记录
结构。编号的数据类型是什么?我希望它不是
char*
@harper是的,我检查了它,也检查了3。文件两个都正常。您在失败的文件上看到您自己的错误消息了吗?@Jongware否,终端上没有消息,好像没有问题。您是否检查了您的第一个文件是否具有预期的记录数?您可能会遇到内存泄漏问题,调用
malloc
1000000次。您没有检查
malloc()
是否成功的原因是什么?另外,请向我们展示
结构记录
结构。编号的数据类型是什么?我希望它不是
char*
@harper是的,我检查了它,也检查了3。文件两个都正常。您在失败的文件上看到您自己的错误消息了吗?@Jongware否,终端上没有消息,好像没有问题。您是否检查了您的第一个文件是否具有预期的记录数?您可能会遇到内存泄漏问题,调用
malloc
1000000次。您没有检查
malloc()
是否成功的原因是什么?另外,请向我们展示
结构记录
结构。编号的数据类型是什么?我希望它不是
char*
@harper是的,我检查了它,也检查了3。这应该是一个注释。是的,我为newRecord->number分配了空间,我不明白为什么是1。三,。没问题,只有2个。有问题。我也使用linux和gcc进行编译,我不知道如何调试它。你有什么建议吗?一个很明显的优化方法是将文件打开并关闭循环。@Jongware:当然没有考虑优化让OP做iti忘记使用free()。非常感谢你。是的,我为newRecord->numbers分配了空间,我不明白为什么是1。三,。没问题,只有2个。有问题。我也使用linux和gcc进行编译,我不知道如何调试它。你有什么建议吗?一个很明显的优化方法是将文件打开并关闭循环。@Jongware:当然没有考虑优化让OP做iti忘记使用free()。非常感谢你。是的,我为newRecord->numbers分配了空间,我不明白为什么是1。三,。没问题,只有2个。有问题。我也使用linux和gcc进行编译,我不知道如何调试它。你有什么建议吗?一个很明显的优化方法是将文件打开并关闭循环。@Jongware:当然没有考虑优化让OP做iti忘记使用free()。非常感谢你。行得通。这应该是一个评论。是的,我知道