C FILE fopen fputs strcat, write content to file via FILE;

#include <stdio.h>
#include <stdlib.h>
#include <uuid/uuid.h>
#include <string.h>

void retrieveUuidVia(char *uuidValue)
{
    uuid_t newUUID;
    uuid_generate(newUUID);
    uuid_unparse(newUUID, uuidValue);
}


int main()
{
   
    FILE7();
    return 0;    
}

 void FILE7()
{
    FILE *fp = fopen("log.txt", "w+");
    if (fp == NULL)
    {
        exit(EXIT_FAILURE);
    }

    char *uuidStr = (char *)malloc(40);
    char *comma = ",";
    char *newLine="\n";

    for (int i = 0; i < 100000000; i++)
    {
        char *str = (char *)malloc(50);
        retrieveUuidVia(uuidStr);
        char *numStr = (char *)malloc(5);
        sprintf(numStr, "%d", i);
        strcat(numStr,comma);
        strcat(str,numStr);
        strcat(str,uuidStr);
        strcat(str,newLine);  
        fputs(str, fp);
        free(numStr);
        free(str);

        if (i % 1000000 == 0)
        {
            printf("I=%d\n", i);
        }
    }
    fclose(fp);
}

 

 

 

 

posted @ 2021-11-28 13:13  FredGrit  阅读(36)  评论(0编辑  收藏  举报