一、创建自己的头文件
- 在当前目录下创建一个my_head.h
- 将这个文件移动到/usr/include目录
#ifndef __MY_HEAD_H__
#define __MY_HEAD_H__#include <stdio.h>
#include <errno.h>
#include <string.h>#define PRINT_ERR(s) do{\printf("%s %s %d\n",__FILE__,__func__,__LINE__);\perror(s);\return -1;\}while(0)#endif
二、fgets(从文件中获取一个字符)
#include <stdio.h>
int fgets(FILE *stream);
功能:从stream代表的文件中获取一个字符
参数:stream:文件指针
返回值:返回获取到的字符,失败或读到文件结尾返回EOF
//fgetc获取一个字符之后,光标会自动向后偏移一个字节
fgets(stdin) == getchar()
fgetc使用示例
#include <my_head.h>int main(int argc, const char *argv[])
{FILE *fp = fopen("./b.c","r");if(NULL == fp){PRINT_ERR("fopen b.c error");}char ch = 0;if(EOF == (ch = fgetc(fp))){printf("fgetc error\n");return -1;}printf("ch = %c\n",ch);//输出if(EOF == (ch=fgetc(fp))){printf("fgetc error\n");return -1;}printf("ch = %c\n",ch);//输出return 0;
}
运行结果:
三、fputs (向文件中放入一个字符)
#include <stdio.h>
int fputc(int c,FILE *stream);
功能:将字符串c写入到stream代表的文字中
参数:c:要写入的字符stream:文件指针
返回值:成功返回写入字符的ascii码,失败返回EOF
fputc应用实例
#include <my_head.h>int main(int argc, const char *argv[])
{FILE *fp = fopen("./b.c","r+");if(NULL == fp){PRINT_ERR("fopen error");}fputc('y',fp);fputc('z',fp);fclose(fp);return 0;
}
运行结果:
四、fgets(从文件中获取字符)
#include <stdio.h> -- 所需头文件
char *fgets(char *s,int size,FILE *stream);
功能:从stream中获取字符串,获取size-1个字符(补尾0),遇到换行符会停止,同时会将换行符读取到s中。
参数:s:保存获取到的字符串的字符数组的首地址size:要获取的字符串的长度stream:成功返回读取到的字符串,失败或者读取到文件结尾返回NULL
fgets代码示例:
#include <my_head.h>int main(int argc, const char *argv[])
{FILE *fp = fopen("./b.c","r+");if(NULL == fp){PRINT_ERR("fopen error");}char buf[128] = {0};fgets(buf,sizeof(buf),fp);buf[strlen(buf)-1] = '\0';//清除换行符printf("buf = [%s]\n",buf);return 0;
}
运行结果:
五、fputs(向文件中写入字符)
#include <stdio.h> -- 所需头文件
int puts(const char *s,FILE *stream);
功能:向stream中放入s这个字符串
参数:s:要放入的字符串的首地址stream:要操作的文件
返回值:成功返回非负数,失败返回EOF
fputs示例代码
#include <my_head.h>int main(int argc, const char *argv[])
{FILE *fp = fopen("b.c","r+");if(NULL == fp){PRINT_ERR("fopen error");}char buf[128] = {0};//stdin从终端输入字符串fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1] = '\0';//使用fputs将内容输入到fp指针指向的文件中fputs(buf,fp);fclose(fp);return 0;
}
运行结果:
六、获取系统时间
man man
#inlcude <time.h>
time_t time(time_t *tloc);
功能:返回的是1970年1月1日,0:0:0到现在的秒数
参数:tloc:保存获取到的秒数的变量的首地址
返回值:成功返回获取到的秒数,失败返回(time_t)-1 --- 强制类型转换(time_t)用法1:
time_t tm = 0;
tm = tiem(NULL);用法2:
time_t tm = 0;
time(&tm);
time函数应用实例
#include <my_head.h>
#include <time.h>int main(int argc, const char *argv[])
{//用法1time_t tm1 = 0;tm1 = time(NULL);printf("time = [%ld]\n",tm1);//用法2time_t tm2 = 0;time(&tm2);printf("time = [%ld]\n",tm2);return 0;
}
运行结果:
6.1 localtime函数的使用
struct tm *localtime(const time_t *timep);
功能:将time函数获取的秒数转化为实际的时间(人能看懂的)
参数:timep:time函数获取到的秒数
返回值:struct tm {int tm_sec; 秒数 0 ~ 60 int tm_min; 分钟数 0~59 int tm_hour; 小时数 0~23 int tm_mday; 一个月的第几天(0~31)int tm_mon; 月份 (0-11) 用的时候需要+1int tm_year; Year - 1900 用的时候需要+1900int tm_wday; 一个星期的第几天 int tm_yday; 一年的第几天 };
localtime函数使用示例
#include <my_head.h>
#include <time.h>int main(int argc, const char *argv[])
{struct tm *nice_time;time_t tm = 0;time(&tm);nice_time = localtime(&tm);//输出printf("%d年%d月%d日 %d时%d分%d秒\n",\nice_time->tm_year+1900,nice_time->tm_mon+1,\nice_time->tm_mday,nice_time->tm_hour,\nice_time->tm_min,nice_time->tm_sec);return 0;
}
运行结果: