zoukankan      html  css  js  c++  java
  • C语言环境下的文件读写

    目的:熟悉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
    ");
    20     }
    21     fclose(fp);
    22 }
    23 
    24 void main(void)
    25 {
    26     printf("start.
    ");
    27     test_write();
    28     printf("end.
    ");
    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
    ");
    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
  • 相关阅读:
    使用Bootstrap后,关于IE与Chrome显示字体的问题
    利用百度接口,识别身份证
    双日历日期选择控件
    回复一个朋友:如何理解委托
    IIS7增加mine类型,以便可以访问apk
    关于SqlBulkCopy SQL批量导入需要注意,列名是区分大小写的
    关于取表中id最大值+1的select语句,哪种效率更高?
    MySQL中如何分析查询语句
    判断同名股票是否存在的MyBatis查询函数写法
    Thymeleaf中model设一个值 页面显示此值 JS取此值
  • 原文地址:https://www.cnblogs.com/mrsandstorm/p/9229285.html
Copyright © 2011-2022 走看看