16.7.25

C programming challenge exercise 2025-07-16-001



Write a program in C which will generate a HTML file with a name BlogPostYYYYMMDD-001.html.

Opening the generated HTML  file in a browser should render a blog post for the day.

There should not be any errors in the developer console.

REQUIREMENTS 



Requirements to Fulfill

1. Language

You must write the program in C.

2. File Creation

The program should "create a new HTML file" using C code.

The filename must follow this format:

BlogPostYYYYMMDD-001.html

    `YYYY` = current year (e.g., 2025)

   `MM` = current month (e.g., 07 for July)

   `DD` = current day (e.g., 16 for the 16th day)

So if today's date is 2025-07-16, the file name should be:

  BlogPost20250716-001.html

3. HTML Content

The content written into the HTML file should be:

  Proper HTML5 document

  Should include at least:


    A `<!DOCTYPE html>` declaration

    `<html>`, `<head>`, `<body>` tags

    A `<title>` tag in `<head>`

    A `<h1>` heading (blog title)

    A couple of `<p>` (paragraphs) as the blog content

4. Valid Output in Browser

When the generated HTML file is opened in a browser:

  It should "render a blog post" cleanly (no blank or broken layout)

  Should "display today's date" in the content or title

5. No Errors in Developer Console

The generated HTML must be:

Syntactically correct

Standards-compliant

When you open the browser console (`F12` → Console tab):

 There should be:

    No red errors

    No JavaScript errors

    No invalid tag warnings

6. Portable and Safe Code


  * The C program should:

  * Use standard libraries like `stdio.h`, `time.h`

  * Properly check if the file was created successfully

  * Close the file at the end

Summary Table


Requirement                       | Description                                 

-------------------------------------------------------------------------------------

Language                            | C                                           

Output File                         | BlogPostYYYYMMDD-001.html                   

HTML Content                  | Valid HTML with title, heading, paragraph   

Browser Behavior              | Renders clean blog post                     

Console Errors                   | No JavaScript or HTML errors                

Code Quality                      | Use file handling and date functions safely 

STEPS TAKEN 



1. Headers 

#include <stdio.h>
#include <time.h>

I included these because I need to do file stuff (stdio.h) and grab the current date (time.h). Without these, the program throws tantrums.

2. Let’s Get This Party Started

int main() {

This is where everything begins. Like literally the heart of the code.

3. What Day is it Today?

time_t t = time(NULL);
struct tm tm = *localtime(&t);

This part is asking my computer: “Yo, what’s the date and time?”
It gives me the full date, and I save it in a format I can use.

4. Naming the File All Fancy

char filename[100];
sprintf(filename, "BlogPost%04d%02d%02d-001.html", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);

Here I’m making the file name like BlogPost20250719-001.html.
Why the +1900? Because tm_year is weird — it starts from 1900. 
Same with months — January is 0. Why? Don’t ask.

5. Let’s Create This File

FILE *fp = fopen(filename, "w");
if (fp == NULL) {
    printf("Error creating the file!\n");
    return 1;
}

Okay, so I’m opening a file for writing. If something goes wrong (like the PC is mad at me), it prints an error and stops.

6. Writing the Blog Magic 

fprintf(fp,
    "<!DOCTYPE html> ... </html>\n",
    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday
);

This is where I dump a whole HTML page into the file.
It has:

  • A cool title with the date,

  • A header,

  • Some nice styles,

  • And a message saying I wrote this with code and coffee .

7. Done Writing. Close the File!

fclose(fp);

Boom. File’s closed. Job’s done. Time to flex. 

8. Tell Me It Worked, Please

printf("Blog post generated successfully: %s\n", filename);

If everything went well, I get this nice success message with the file name. It feels good.

9. That’s All Folks

return 0;
}

The program exits peacefully. No drama.

So what happens?

You run this C code → It grabs today’s date → Makes a file called something like BlogPost20250723-001.html → Fills it with a little HTML blog post → You open it in your browser → You’re a blogger now 



INPUT

No manual input needed.
The program fetches the current date from your system.

Let’s assume today’s date is 19th July 2025.

OUTPUT

Console Output

When you run the program, you’ll see this in the terminal/console:

Blog post generated successfully: BlogPost20250719-001.html

File Created

A new file is created in your program folder named:

BlogPost20250719-001.html

HTML Content Inside the File

Open the file in any web browser, and it will show something like:

Blog Preview in Browser

Title of the page (in browser tab):

My Blog Post - 2025/07/19

What the webpage looks like:

Inside the browser:

Blog Post for 2025/07/19

Welcome to my blog! This is an auto-generated post created using a C program.
Today is a great day to write some code.
 

THE ACTUAL PROGRAM 




#include <stdio.h>

#include <time.h>


int main() {

    // Get current date

    time_t t = time(NULL);

    struct tm tm = *localtime(&t);

    

    // Create file name in BlogPostYYYYMMDD-001.html format

    char filename[100];

    sprintf(filename, "BlogPost%04d%02d%02d-001.html", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);

    

    // Open file for writing

    FILE *fp = fopen(filename, "w");

    if (fp == NULL) {

        printf("Error creating the file!\n");

        return 1;

    }


    // Write HTML content

    fprintf(fp,

        "<!DOCTYPE html>\n"

        "<html lang=\"en\">\n"

        "<head>\n"

        "    <meta charset=\"UTF-8\">\n"

        "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n"

        "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"

        "    <title>My Blog Post - %04d/%02d/%02d</title>\n"

        "    <style>\n"

        "        body { font-family: Arial, sans-serif; background: #f4f4f4; padding: 20px; }\n"

        "        .blog-container { background: white; padding: 20px; border-radius: 10px; max-width: 700px; margin: auto; }\n"

        "        h1 { color: #333; }\n"

        "        p { line-height: 1.6; }\n"

        "    </style>\n"

        "</head>\n"

        "<body>\n"

        "    <div class=\"blog-container\">\n"

        "        <h1>Blog Post for %04d/%02d/%02d</h1>\n"

        "        <p>Welcome to my blog! This is an auto-generated post created using a C program. Today is a great day to write some code .</p>\n"

        "        <p>Stay tuned for more posts. Until then, happy coding! 🚀</p>\n"

        "    </div>\n"

        "</body>\n"

        "</html>\n",

        tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,

        tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday

    );


    // Close file

    fclose(fp);


    printf("Blog post generated successfully: %s\n", filename);

    return 0;

}


No comments:

Post a Comment

rating System

Loading...

Understanding Arrays in C

The Bookshelf Analogy & Book-Author Example Arrays are one of the most essential concepts in C programming. They let you store multiple ...