c - 在 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(¤tTime);
struct tm *myTime = localtime(¤tTime);
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);
}
我应该将时间转换为字符串还是什么?有任何想法吗?谢谢
解决方案
推荐阅读
- python - 如何从 Python 脚本调用 R Markdown knit 函数
- python - 光标跟踪 Python Tkinter
- python - 在 Python 中基于外部生成的表(即 csv 文件或数据框)动态选择数据框的元素
- rxjs - 如何更新 BehaviorSubject 中 1 个对象中的 1 个属性并让所有观察者知道
- javascript - 如何使用 useContext 更改 Context 的值?
- python - 从熊猫数据框中的行子集中计算平均值:groupby 还是 for 循环?
- c# - 将 Deedle 数据框转换为自定义类的 C# 列表
- python - 在没有结果的情况下用 Python 计算曼哈顿距离
- android - 对象实现 Parcelable 时未找到类异常
- docker - Golang Docker 容器未在 Docker-Compose 中重新启动