首页 > 解决方案 > 在 C 中将时间转换为字符串并添加到 Txt

问题描述

我有一个问题,我有函数 saveDatabase 可以添加数据并将数据发送到 txt,savaDatabase 有 7 个参数,但是我对日期和时间参数有疑问,但我会向您展示 1 个示例,

int time 参数将从 timeSystem 函数中获取数据。timeSystem 将从设备获取本地时间,然后返回它的值,但是当我这样做时,时间参数将返回随机数。

例如,当我使用下面的代码运行程序时,我希望 txt 文件中的结果如下所示:

5, 15/11/21 , 12, 13 , 5000,ticketDiscount 函数结果,ticketCounter的结果

但是当我检查 txt 文件时,我得到如下信息:5, 56 , 12, 13, 5000,ticketDiscount 函数结果,ticketCounter的结果

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

void saveDatabase();
int timeSystem();

int main(void)
{

   saveDatabase( 5, timeSystem(), 12, 13, 50000, ticketDiscount(1, 2), ticketCounter(5, 3) );

}

int timeSystem(){
    char line[500] = "\xb3";
    char dateCatch[100];
    time_t currentTime;
    time(&currentTime);
    struct tm *myTime = localtime(&currentTime);
    return ("%02i:%02i:%02i", myTime->tm_hour, myTime->tm_min, myTime->tm_sec);
}

void saveDatabase( int date, int time, int ticketRegular, int ticketVip, int totalPrices, int discount, int totalPayment ){
    FILE *database;

    if ((database = fopen("./database.txt", "a")) == NULL)
        {
            // Useless
            printf("ERROR 505 -- DATABASE FILE NOT FOUND -- ");
            printf("Please Contact Our Manager");
            exit(1);
        } 
    
    fprintf(database, "\xb3 Date : %d | Time : %i | Regular : %d | VIP : %d | Prices : %d | Discount : %d | Payment : %d \xb3\n", date, time, ticketRegular, ticketVip, totalPrices, discount, totalPayment);
    fclose(database);
    
}

我应该将时间转换为字符串还是什么?有任何想法吗?谢谢

标签: cstringfunctiontimetime-series

解决方案


推荐阅读