Search code examples
htmlcfileline-breaks

How to change a line in my .html file which created and written in c?


I used this -> fprintf(file, "\r\n"); but doesn't change line.

I have a webserver written in c. After the connection I create an html file with content like this:

This file was saved at :Fri Apr 19 00:49:43 2019 (line break!)
Please click here.

Code:

file = fopen("testvideo.html", "w");

if (file == NULL)
{
    /* File not created hence exit */
    printf("Unable to create file.\n");
    exit(EXIT_FAILURE);
}


time(&tv);
timestr = ctime(&tv);
fprintf(file, "<html> The file was saved at:</html>");
fprintf(file, timestr);
fprintf(file, "\r\n");
fprintf(file,"<html> Please click :<a href=\"http://whatevevevever.com/testvideo.mp3\"\>HERE<a></html>");
fclose(file);

Solution

  • You have two issues here, one of which is definitely key:

    1. In HTML a line break is <br> not \r\n
    2. Your HTML isn't correct (or valid). I won't give an HTML tutorial here but you need to use the correct structure for your HTML to be sure browsers will render it correctly.

    Corrected code:

    if (file == NULL)
    {
        /* File not created hence exit */
        printf("Unable to create file.\n");
        exit(EXIT_FAILURE);
    }
    
    
    time(&tv);
    timestr = ctime(&tv);
    fprintf(file, "<html> <body>The file was saved at:");
    fprintf(file, timestr);
    fprintf(file, "<br>Please click :<a href=\"http://whatevevevever.com/testvideo.mp3\"\>HERE<a></body></html>");
    fclose(file);