I'm trying to make a utility for my stream. Basically, OBS can read from a text file, so I'm coding a program that updates a text file with the current time.
The program would accept a string to append in front of the actual time. It's defined in main(), and passed to writeTime(), which executes every second and does the writing.
However, something's going wrong, and I can't show the prefix even if I define it. I think there's something wrong with the way that I pass the argument to writeTime(), but I can't figure out what the issue is.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
// This program writes the current time into a text file every second.
int writeTime(short int timezone, char prefix[], short int h24){
    // Open the text file, in which to write
    FILE *filePointer;
    filePointer = fopen("clock.txt", "w+");
    if (filePointer == NULL) {
        printf("The file clock.txt failed to open.");
    } else {
        // THIS PART DOESN'T QUITE WORK WITH THE PREFIX:
        time_t now = time(NULL); // get system time in seconds from 1970-01-01
        int timeOfDay = (now + timezone * 3600) % 86400;
        short int hour = timeOfDay / 3600;
        short int minute = timeOfDay % 3600 / 60;
        short int second = timeOfDay % 60;
        if (h24) { // h24 is the 24 hour time flag
            printf("%s %02i:%02i.%02i\n", prefix, hour, minute, second);
            fprintf(filePointer, "%s %02i:%02i.%02i", prefix, hour, minute, second);
        } else {
            char* ampm;
            if (hour < 12) {
                ampm = "AM";
            } else {
                ampm = "PM";
            }
            printf("%s %02i:%02i.%02i %s\n", prefix, hour%12, minute, second, ampm);
            fprintf(filePointer, "%s %02i:%02i.%02i %s", prefix, hour%12, minute, second, ampm);
        }
    }
    fclose(filePointer);
    return 0;
}
int main(int argc, char **argv){
    // Flags : 00000HPZ, 1 if set from command linearguments
    // Z: timezone, P: prefix, H: 24h mode
    unsigned short int flags = 0;
    short int timezone = 0;
    char prefix[64] = "";
    short int h24 = 0;
    // This part is meant to get parameters from command line arguments to save time for reuse
    // -z: timezone; -p: prefix string; -24: 24h time (1 or 0 = on or off)
    for (int i = 1; i < argc; ++i) {
        if (0 == strcmp(argv[i], "-z")) {
            timezone = (int) strtol(argv[++i], (char **)NULL, 10);
            flags |= 1;
            printf("Timezone UTC%+i\n", timezone);
        } else if (0 == strcmp(argv[i], "-p")) {
            strcpy(prefix, argv[++i]);
            flags |= 2;
            printf("Prefix %s\n",prefix);
        } else if (0 == strcmp(argv[i], "-24")) {
            h24 = (int) strtol(argv[++i], (char **)NULL, 10);
            flags |= 4;
            printf("24h %i\n", h24);
        }
    }
    // User input for parameters not gotten from arguments:
    if (!(flags & 1)) {
        printf("Enter your timezone, as offset from UTC (e.g. East Coast US = -5, or -4 during DST): ");
        scanf("%i", &timezone);
        printf("UTC%+i\n", timezone);
    }
    if (!(flags & 1 << 1)) {
        printf("Enter your prefix (e.g. \"Local time:\", up to 64 characters) ");
        // flush the input buffer: https://stackoverflow.com/questions/7898215/how-to-clear-input-buffer-in-c
        int c;
        while ((c = getchar()) != '\n' && c != EOF) {}
        gets(prefix);
        puts(prefix);
    }
    if (!(flags & 1 << 2)) {
        printf("Enter \"1\" to enable 24 hour mode, and \"0\" to enable 12 hour mode. ");
        scanf("%i", &h24);
        printf("24h %i\n", h24);
    }
    // Main loop
    while (1) {
        // AM I DOING THIS PART RIGHT???
        writeTime(timezone, prefix, h24);
        sleep(1);
    }
    return 0;
}
 
     
    