首页 > 技术文章 > C语言环境下的文件读写

mrsandstorm 2018-06-26 15:19 原文

目的:熟悉C的文件读写,emwin中用到log输出

环境:VC6.0++

补充:

  1. 据说VC6.0不支持多参数宏定义,支持要C99以上才可以
  2. vc6.0库函数和头文件

代码:

其他测试main.c文件如下

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void test_write(void)
 5 {
 6     FILE *fp = NULL;
 7     char name[] = "mrsandstorm";
 8 
 9     fp = fopen("test.txt","w");
10 
11     if( fp == NULL)
12     {
13         printf("can't open the file");
14         return;
15     }
16 
17     if (fwrite(name, sizeof(char), strlen(name) ,fp) != strlen(name))
18     {
19         printf("file write error\n");
20     }
21     fclose(fp);
22 }
23 
24 void main(void)
25 {
26     printf("start.\n");
27     test_write();
28     printf("end.\n");
29 }

 

main.c文件内容如下

 1 #include "includes.h"
 2 
 3 #ifdef USE_EMWIN_VC6
 4 #define MAIN_DEBUG_LOG LOG_DEBUG_SET_LEVLE(1)
 5 #else
 6 #define MAIN_DEBUG_LOG (1)
 7 #endif
 8 
 9 int main(void)
10 {
11     print_cond(MAIN_DEBUG_LOG,"END\n");
12 
13     return 0;
14 }

 

 

includes.h文件内容如下

 1 #ifndef __INCLUDES_H__
 2 #define __INCLUDES_H__
 3 
 4 #include <stdio.h>
 5 #include <stdarg.h>
 6 #include <string.h>
 7 #include <time.h>
 8 
 9 #include "fs_log.h"
10 
11 #define USE_EMWIN_VC6
12 
13 #endif

 

 

fs_log.c文件内容如下

 1 #include "fs_log.h"
 2 
 3 void write_log (const char *format, ...)
 4 {
 5     va_list arg;
 6     int done;
 7     time_t time_log;
 8     struct tm* tm_log;
 9     FILE* pFile = NULL;
10 #if LOG_HAVE_WINDOW_OUT
11     char buff[300];
12     int off = 0;
13 #endif
14 
15     va_start (arg, format);
16 
17     time_log = time(NULL);
18     tm_log = localtime(&time_log);
19 
20     pFile = fopen(LOG_FILE, "a");
21 
22     fprintf(pFile,"%04d-%02d-%02d %02d:%02d:%02d :",
23         tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday,
24         tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec);
25     done = vfprintf (pFile, format, arg);
26     va_end (arg);
27     fflush(pFile);
28 
29     fclose(pFile);
30 
31 #if LOG_HAVE_WINDOW_OUT
32     memset(buff, 0, sizeof(buff));
33     off = sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d [%s][%d]:",
34         tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday,
35         tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec);
36     sprintf(buff+off, format, arg);
37     printf("%s",buff);
38 #endif
39 
40     return;
41 }
42 
43 void write_log_cond(char valve, char *file_nameconst, int len, char *format, ...)
44 {
45     va_list arg;
46     int done;
47     time_t time_log;
48     struct tm* tm_log;
49     FILE* pFile = NULL;
50 #if LOG_HAVE_WINDOW_OUT
51     char buff[300];
52     int off = 0;
53 #endif
54 
55     if (valve == 0)
56     {
57         return;
58     }
59     va_start (arg, format);
60 
61     time_log = time(NULL);
62     tm_log = localtime(&time_log);
63 
64     pFile = fopen(LOG_FILE, "a");
65 
66     fprintf(pFile,"%04d-%02d-%02d %02d:%02d:%02d [%s][%d]:",
67         tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday,
68         tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec,
69         file_nameconst,len);
70     done = vfprintf (pFile, format, arg);
71     va_end (arg);
72     fflush(pFile);
73 
74     fclose(pFile);
75 
76 #if LOG_HAVE_WINDOW_OUT
77     memset(buff, 0, sizeof(buff));
78     off = sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d [%s][%d]:",
79         tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday,
80         tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec,
81         file_nameconst,len);
82     sprintf(buff+off, format, arg);
83     printf("%s",buff);
84 #endif
85 
86     return;
87 }

 

 

fs_log.h文件内容如下

#ifndef __FS_LOG_H__
#define __FS_LOG_H__

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>

#define LOG_FILE "./runlog.txt"
#define LOG_HAVE_WINDOW_OUT     (1)

#define SHORT_FILE strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__
#define LOG_DEBUG_SET_LEVLE(a)  (a),(SHORT_FILE),(__LINE__)

#define print_deb    write_log
#define print_info    write_log
#define print_war    write_log
#define print_err    write_log
#define print_cond    write_log_cond

extern void write_log (const char *format, ...);
extern void write_log_cond (char valve, char *file_nameconst, int len, char *format, ...);

#endif

 

推荐阅读