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

 

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

 

 

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

 

上一篇:20 件程序员必须知道的事


下一篇:PAT A1048