C:我传递的字符串错了吗?

标签 c c-strings

我正在尝试为我的流创建一个实用程序。基本上,OBS 可以从文本文件中读取,所以我正在编写一个程序,用当前时间更新文本文件。

程序会接受一个字符串附加在实际时间之前。它在 main() 中定义,并传递给 writeTime(),它每秒执行一次并进行写入。

但是,出问题了,我定义了前缀也显示不出来。我认为我将参数传递给 writeTime() 的方式有问题,但我无法弄清楚问题出在哪里。

我的代码:

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

最佳答案

是的,那应该没问题。但是你在到达那里之前正在使用 scanf

short int h24;
...
scanf("%i", &h24);

%i 假定 int 而不是 short。所以它将 4 个字节写入一个 2 字节的变量。那会溢出到你的字符串中。它把我的 prefix[0] 改成了 0,所以字符串的长度是 0。

改用 scanf("%h", &h24);

关于C:我传递的字符串错了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55271336/

相关文章:

c - 如何使用 malloc 在 NASM 程序集中创建一个新的字符数组

c - 为什么编译器坚持在这里使用被调用者保存的寄存器?

char 数组与 char 指针

julia - 从 julia 的字节向量中读取空终止字符串

c++ - extern "C"函数访问代码

mysql - snprintf 截断最后一个符号

c - 如何打印单个字符的可变长度字符串

c - 如何在 C 中输出冒泡排序的二维字符串数组?

c - Lua C 扩展 : how to set metatable on new library

python - 如何计算格式非常特殊的文件中的对象/子字符串?