经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
C语言向txt文件写入当前系统时间(Log)
来源:cnblogs  作者:wowpH  时间:2019/10/17 9:25:10  对本文有异议

 

程序实现很简单,涉及到的只有两个知识点。

1、文件最最基本的打开关闭;
2、系统时间的获取。

代码是在VS2019环境下编写的,部分函数在其他编译器中会无法识别。这个就只能需要自己去查找对应的函数简单修改即可。

如果遇到问题,欢迎大家提出来一起讨论。

文件操作:

先来介绍一下 文件操作 的基本方法。
操作文件必须遵从的过程是:打开读写关闭
一个文件首先要知道的是 文件名,当然你也可以新建一个文件。
如果是不同路径下的文件,还需要知道文件的路径。为了方便,本文采用同一目录下操作来说明。

只知道文件名并没什么用处,重要的是要通过文件名得到 文件结构体 FILE 的变量,里面会保存文件的一些基本但重要的信息。然后通过它来进行下一步的读写。

先来看一下文件操作的简单代码示例,后面再介绍函数的功能。

  1. 1 #include <stdio.h>
  2. 2
  3. 3 int main(void) {
  4. 4 FILE* stream;// 文件指针
  5. 5 errno_t err;// 错误码
  6. 6 char* filename = "file.txt";// 文件名
  7. 7 // 打开,以在文件末尾追加(append)数据的形式打开文件filename
  8. 8 err = fopen_s(&stream, filename, "a");
  9. 9 if (err == 0) {
  10. 10 printf("文件 %s 打开成功\n", filename);
  11. 11 } else {
  12. 12 printf("文件 %s 打开失败\n", filename);
  13. 13 }
  14. 14 // 读写
  15. 15 if (stream != NULL) {
  16. 16 // 向文件末尾追加一段文字。
  17. 17 fprintf(stream, "%s", "文件操作成功!\n");
  18. 18 }
  19. 19 // 关闭
  20. 20 if (stream != NULL) {
  21. 21 // 关闭文件指针,先要判断是否为空
  22. 22 err = fclose(stream);
  23. 23 if (err == 0) {
  24. 24 printf("文件 %s 关闭成功\n", filename);
  25. 25 } else {
  26. 26 printf("文件 %s 关闭失败\n", filename);
  27. 27 }
  28. 28 }
  29. 29 }
  1. 打开文件用到的是 fopen_s 函数,函数语法如下:
  1. errno_t fopen_s(
  2. FILE** pFile, // 一个指向文件指针的指针,它将接收指向打开的文件的指针。
  3. const char *filename, // 文件名
  4. const char *mode // 文件打开的方式,方式有很多种,可参考上面的链接。
  5. );

如果打开成功返回 0,否则返回相应的错误码。

写数据用到的函数是 fprintf,语法如下:

  1. int fprintf(
  2. FILE *stream, // 文件指针
  3. const char *format [, // 格式控制字符串
  4. argument ]... // 可选参数
  5. );

类似 printf 函数,只不过多了个文件指针而已。很好理解。
返回值是写入的字节数,通常用不到,可忽略。
当然,不止这一个函数可以写操作,还有 fwrite 函数,fputs 函数等等。

关闭文件用的是 fclose 函数,语法如下:

  1. int fclose(
  2. FILE *stream // 文件指针
  3. );

关闭成功,返回 0,错误返回 EOF

有了上面这些知识,你就可以向文件写入一个 Hello World 了。其实还有很多内容,但是如标题所示,现在要写的是时间。是个字符串,只需要用到这些内容。其他的可参考 UCRT alphabetical function reference

获取时间:

现在来看看怎么获取当前时间。直接上代码,再说明函数语法(其实看注释就够了)。

  1. #include <stdio.h>
  2. #include <time.h>
  3. int main(void) {
  4. char timebuf[21];// 字符串形式保存时间
  5. time_t ltime;// 时间戳
  6. struct tm today;// 本地时间结构体
  7. errno_t err;// 错误码
  8. time(&ltime);// 获取系统时间
  9. err = _localtime64_s(&today, &ltime);// 转换为本地时间
  10. if (err != 0) {
  11. printf("无法转换为本地时间\n");
  12. exit(1);
  13. }
  14. // 将时间转换为字符串,自定义格式
  15. strftime(timebuf, 21, "%Y/%m/%d %T%n", &today);
  16. printf(timebuf);
  17. }

用到的函数有:time_localtime64_sstrftime

time 语法如下:

  1. time_t time( time_t *destTime ); // 获取系统时间,参数是指向时间的存储位置的指针

返回值是从 1970/01/01 00:00:00 以来经过的秒数。有错误返回 -1

_localtime64_s 语法如下:

  1. errno_t _localtime64_s(
  2. struct tm* tmDest, // 指向要填充的时间结构体
  3. __time64_t const* sourceTime // 指向时间的存储位置的指针
  4. ); // 将时间转换为本地时间结构体

转换成功返回 0,否则返回对应的错误码。

strftime 语法如下:

  1. size_t strftime(
  2. char *strDest, // 保存时间的字符串
  3. size_t maxsize, // strDest指向的大小
  4. const char *format, // 格式控制串,参照使用手册。
  5. const struct tm *timeptr // 时间结构体
  6. );

返回填充的字符数,如果超过了 maxsize,返回 0


到这里差不多就已经掌握了简单的写入时间操作。可以试试自己写一写。

不会也没关系,下面的代码是封装好的。可以直接调用。

函数原型:

  1. // 功能:向txt文件filename末尾追加格式为YYYY/MM/DD HH:MM:SS的时间
  2. // filename是文件名,文件名本身不带双引号
  3. void log_time_filename(char const* filename);
  1. // 功能:向stream指向的文件末尾追加格式为YYYY/MM/DD HH:MM:SS的时间
  2. // stream必须打开
  3. void log_time_stream(FILE* stream); // 参数是文件指针
  1. // 功能:向txt文件filename末尾追加格式为YYYY/MM/DD HH:MM:SS的时间并换行
  2. // filename是文件名,文件名本身不带双引号
  3. void log_time_line_filename(char const* filename);
  1. // 功能:向stream指向的文件末尾追加格式为YYYY/MM/DD HH:MM:SS的时间并换行
  2. // stream必须打开
  3. void log_time_line_stream(FILE* stream);

log.h文件:

将代码保存到与 调用文件 在同一级目录下的 log.h 文件中。有四个外部函数可供调用。

  1. /*********************************************************************
  2. 文件: log.h
  3. 环境: Windows 10
  4. IDE: Visual Studio 2019
  5. 版本: 1.5.0.1910
  6. 功能: 1、向 <文件名.txt> 文件末尾追加当前系统时间
  7. 2、向 <FILE* stream> 指向的文件末尾追加当前系统时间
  8. 备注: 1、目前只有默认日期格式,即YYYY/MM/DD hh:mm:ss
  9. 2、支持换行和不换行两种形式
  10. 日期: 2019年10月16日15:42:15
  11. 作者: wowpH
  12. *********************************************************************/
  13. #pragma once
  14. #pragma message("log.h - wowpH - 1.5.0.1910")
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <time.h>
  18. // 时间格式的最大长度
  19. #define MAX_TIME_MODE_SIZE 21
  20. //
  21. // 可调用的函数
  22. //
  23. // 通过文件名追加时间
  24. void log_time_filename(char const* filename);
  25. // 通过文件指针追加时间
  26. void log_time_stream(FILE* stream);
  27. // 通过文件名追加时间,换行
  28. void log_time_line_filename(char const* filename);
  29. // 通过文件指针追加时间,换行
  30. void log_time_line_stream(FILE* stream);
  31. //
  32. // 供内部使用的函数,外部调用可能出错
  33. //
  34. // 向strDest写入当前时间
  35. void get_local_time(char* strDest);
  36. // 打开文件
  37. void open_file(FILE** stream, char const* filename);
  38. // 关闭文件
  39. void close_file(FILE* stream);

log.c文件:

将代码保存到与 调用文件 同一级目录下的 log.c 文件中。

  1. #include "log.h"
  2. const char* null_stream = "文件指针为空,请先打开一个文件!";
  3. const char* error_open = "文件打开失败!";
  4. const char* error_close = "文件关闭失败!";
  5. const char* error_to_localtime = "无法转换为本地时间!";
  6. void log_time_filename(char const* filename) {
  7. FILE* stream = NULL;
  8. open_file(&stream, filename);
  9. log_time_stream(stream);
  10. close_file(stream);
  11. }
  12. void log_time_stream(FILE* const stream) {
  13. if (stream != NULL) {
  14. char time[MAX_TIME_MODE_SIZE];
  15. get_local_time(time);
  16. fprintf(stream, "%s", time);
  17. } else {
  18. printf("%s\n", null_stream);
  19. exit(-1);
  20. }
  21. }
  22. void log_time_line_filename(char const* filename) {
  23. FILE* stream = NULL;
  24. open_file(&stream, filename);
  25. log_time_line_stream(stream);
  26. close_file(stream);
  27. }
  28. void log_time_line_stream(FILE* stream) {
  29. log_time_stream(stream);
  30. fprintf(stream, "\n");
  31. }
  32. static void get_local_time(char* strDest) {
  33. time_t ltime;
  34. time(&ltime);
  35. struct tm today;
  36. errno_t err;
  37. err = _localtime64_s(&today, &ltime);
  38. if (err != 0) {
  39. printf("%s\n", error_to_localtime);
  40. exit(-1);
  41. }
  42. // 将时间转换为字符串,自定义格式
  43. strftime(strDest, MAX_TIME_MODE_SIZE, "%Y/%m/%d %T", &today);
  44. }
  45. static void open_file(FILE** stream, char const* filename) {
  46. errno_t err = 0;
  47. err = fopen_s(stream, filename, "a");
  48. if (err != 0) {
  49. printf("%s\n", error_open);
  50. exit(-1);
  51. }
  52. }
  53. static void close_file(FILE* stream) {
  54. if (stream != NULL) {
  55. errno_t err = 0;
  56. err = fclose(stream);
  57. if (err != 0) {
  58. printf("%s\n", error_close);
  59. exit(-1);
  60. }
  61. }
  62. }

执行结果:

log.jpg

原文链接:http://www.cnblogs.com/wowpH/p/11687385.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号