Search code examples
cfopenfwrite

fopen fprintf dosnt write to file only after closing it


how to enable the writing to file befor closing it , i have this code to write to file , is does it but only after i close the app and it gets to the distractor its part of simple logger. what i need is to see the write updates ( with tail -f )

void init(char *pfileName)
    {
        if(pfileName != NULL)
        {
            fp = fopen(pfileName, "a+");
            if(fp != NULL)
                fseek(fp, 0, SEEK_END);
        }
    }

    void close()
    {
        if(fp != NULL)
            fclose(fp);
        fp = NULL;
    }

void write(char *pType, std::string pMsg, char *pFileName, int lineNo)
    {
        if(fp != NULL)
        {
            fprintf(fp,"%s %s %s %s %d %s\n", pType, __DATE__, __TIME__, pFileName, lineNo, pMsg.c_str());

        }
    }

Solution

  • The IO is likely buffered. Insert fflush(fp) after your write.