我正在尝试为我的流制作实用程序。基本上,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;
}
答案 0 :(得分:2)
是的,那应该很好。但是在到达那里之前您正在使用scanf
short int h24;
...
scanf("%i", &h24);
%i
假定int
而非short
。因此它将4字节写入2字节变量。那溢出到您的字符串。它在我的前缀中将prefix [0]设置为0,因此字符串的长度为0。
改为使用scanf("%h", &h24);
。
答案 1 :(得分:0)
以下建议的代码:
<-- corrected
来查看我所做的修改现在,建议的代码:
#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;
}
终端输出为:
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
....
磁盘文件:clock.txt
包含:
pst
16:32.48
并每秒更新一次