Oğuzhan KAYIŞ
Oğuzhan KAYIŞ

Reputation: 25

C-create a file

I am trying to create 3 different file that includes random records. There is no problem about creating random records. What's more there is no problem about creating 2 of them. But whatever i do, the file dataFile2.dat wasn't created. I am so confused really. Is there anybody who know why?

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

Upvotes: 0

Views: 98

Answers (2)

Giorgi Moniava
Giorgi Moniava

Reputation: 28685

Ok here is final version (indeed using struct might not be needed here, also I avoided using dynamic memory in the first place).

int writeRecordToFile(char*fileName, int numberRecord)
{
    FILE * file= fopen(fileName, "a");
    if (file == NULL)
    {
        puts("Error handled when appending file");
        return -1;
    }

    int i;
    for(i = numberRecord; i > 0; i--)
    {
        struct record newRecord;
        strcpy(newRecord.numbers,"gg");
        strcpy(newRecord.letters,"ww");
        strcpy(newRecord.date,"tt");

        fprintf(file, "%s,%s,%s\n", newRecord.numbers,newRecord.letters,newRecord.date);
    }

    fclose(file);
    return 0;
}

Upvotes: 2

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53036

Your problem is you keep calling fopen inside the loop without even calling fclose, and malloc without calling free so you run out of resources, this should work

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

Also, I don't see the point in using a struct for this, and if randstring is allocating memory dynamically you still have a memory leak.

Upvotes: 1

Related Questions