Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我传错线了吗?_C_C Strings - Fatal编程技术网

C 我传错线了吗?

C 我传错线了吗?,c,c-strings,C,C Strings,我正在尝试为我的流创建一个实用程序。基本上,OBS可以读取文本文件,所以我正在编写一个程序,用当前时间更新文本文件 程序将接受在实际时间之前追加的字符串。它在main()中定义,并传递给writeTime(),后者每秒执行一次并执行写入操作 但是,出现了一些问题,即使我定义了前缀,也无法显示它。我认为我将参数传递给writeTime()的方式有问题,但我无法找出问题所在 我的代码: #include <stdio.h> #include <stdlib.h> #inclu

我正在尝试为我的流创建一个实用程序。基本上,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;
}
#包括
#包括
#包括
#包括
#包括
//该程序每秒将当前时间写入一个文本文件。
int writeTime(短int时区,字符前缀[],短int h24){
//打开要写入的文本文件
文件*文件指针;
filePointer=fopen(“clock.txt”,“w+”);
if(filePointer==NULL){
printf(“文件clock.txt无法打开”);
}否则{
//这部分与前缀不太匹配:
time_t now=time(NULL);//从1970-01-01以秒为单位获取系统时间
int timeOfDay=(现在+时区*3600)%86400;
短时整小时=一天的时间/3600;
短整数分钟=一天的时间%3600/60;
短整数秒=天的时间%60;
如果(h24){//h24是24小时时间标志
printf(“%s%02i:%02i.%02i\n”,前缀,小时,分钟,秒);
fprintf(文件指针,“%s%02i:%02i.%02i”,前缀,小时,分钟,秒);
}否则{
char*ampm;
如果(小时<12){
ampm=“AM”;
}否则{
ampm=“PM”;
}
printf(“%s%02i:%02i.%02i%s\n”,前缀,小时%12,分钟,秒,ampm);
fprintf(文件指针,“%s%02i:%02i.%02i%s”,前缀,小时%12,分钟,秒,ampm);
}
}
fclose(文件指针);
返回0;
}
int main(int argc,字符**argv){
//标志:00000HPZ,如果从命令linearguments设置,则为1
//Z:时区,P:前缀,H:24小时模式
无符号短整型标志=0;
短整数时区=0;
字符前缀[64]=“”;
短int h24=0;
//此部分旨在从命令行参数获取参数,以节省重用时间
//-z:时区;-p:前缀字符串;-24:24小时时间(1或0=开或关)
对于(int i=1;i如果(!(flags&1是的,那应该没问题。但是在你到达那里之前你正在使用scanf

short int h24;
...
scanf("%i", &h24);
%i
假定一个
int
而不是
short
。因此它将4个字节写入一个2字节的变量。这会溢出到您的字符串中。它会将我的前缀[0]替换为0,因此字符串的长度为0


改用
scanf(“%h”和&h24);

以下建议的代码:

#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) 
    {
        perror("The file clock.txt failed to open."); // <-- corrected
    } 

    else 
    {
        // THIS PART DOESN'T QUITE WORK WITH THE PREFIX:
        time_t now = time(NULL); // get system time in seconds from 1970-01-01
        time_t timeOfDay = (now + timezone * 3600) % 86400;
        short int hour = (short)(timeOfDay / 3600); // <-- corrected
        short int minute = (short)(timeOfDay % 3600 / 60); // <-- corrected
        short int second = (short)(timeOfDay % 60); // <-- corrected

        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\n", prefix, hour, minute, second);  // <-- corrected
        } 

        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\n", prefix, hour%12, minute, second, ampm); // <-- corrected
        }
    }
    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 = ( short int) strtol(argv[++i], NULL, 10);  // <-- corrected
            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 = (short int) strtol(argv[++i], NULL, 10);  // <-- corrected
            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("%hi", &timezone);  // <-- corrected
        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: 
        int c;
        while ((c = getchar()) != '\n' && c != EOF) {}

        // replace the call to `gets()` with:
        fgets( prefix, sizeof(prefix), stdin );
        puts(prefix);
    }
    if (!(flags & 1 << 2)) {
        printf("Enter \"1\" to enable 24 hour mode, and \"0\" to enable 12 hour mode. ");
        scanf("%hi", &h24); // <-- corrected
        printf("24h %i\n", h24);
    }

    // Main loop
    while (1) 
    {
        // AM I DOING THIS PART RIGHT???
        writeTime(timezone, prefix, h24);
        sleep(1);
    }

    return 0;
}
  • 干净地编译
  • 执行所需的功能

  • 查找
    为什么不使用系统函数、
    asctime
    strftime
    ctime
    ?通过
    gcc
    编译器运行发布的代码会导致大量警告和错误。例如:
    untitled2.c:67:17:警告:格式“%i”要求参数的类型为“int*”,但参数2的类型为“short int int*”[-Wformat=]
    用于语句:
    scanf(“%i”、&timezone);
    建议更正输入格式说明符。该语句也存在类似的考虑事项:
    scanf(“%i”、&h24)
    关于:
    gets(前缀);
    函数:
    gets()
    已被折旧多年,并从最新版本的C语言中完全删除。建议在编译时使用
    fgets()
    (阅读手册页,因为参数列表不同),始终启用警告,然后修复这些警告。(对于
    gcc
    ,至少使用:
    -Wall-Wextra-Wconversion-pedantic-std=gnu11
    )注意:其他编译器使用不同的选项生成相同的内容建议使用输入格式说明符(对于短int变量)
    %hi
    确实如此。这在很大程度上起了作用!不过我必须删除
    前缀
    末尾的
    \n
    。。。
    Enter your timezone, as offset from UTC (e.g. East Coast US = -5, or -4 during DST): -7
    UTC-7
    Enter your prefix (e.g. "Local time:", up to 64 characters) pst
    pst
    
    Enter "1" to enable 24 hour mode, and "0" to enable 12 hour mode. 1
    24h 1
    
    pst
     16:34.58
    pst
     16:34.59
    pst
     16:35.00
    pst
     16:35.01
    pst
     16:35.02
    pst
     16:35.03
    pst
     16:35.04
    pst
     16:35.05
    pst
     16:35.06
    ....
    
    pst
     16:32.48