当前位置: 首页 > article >正文

库函数相关(上一篇补充)

一、创建自己的头文件 

  1. 在当前目录下创建一个my_head.h
  2. 将这个文件移动到/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[])
{
	//用法1
	time_t tm1 = 0;
	tm1 = time(NULL);
	printf("time = [%ld]\n",tm1);

	//用法2
	time_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) 用的时候需要+1
               int tm_year;   Year - 1900 用的时候需要+1900
               int 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;
}

运行结果:


http://www.kler.cn/news/340970.html

相关文章:

  • WPF中的Window类
  • 【hot100-java】N 皇后
  • JavaScript 网页设计案例与技巧
  • Linux入门3——vim的简单使用
  • GEE 教程:利用Landsat函数计算不同缓冲区内的NDVI,NDWI和EVI的平均值
  • Django makemigrations时出现TypeError: ‘module‘ object is not iterable
  • minio简单使用
  • PostgreSQL中WITH查询公用表表达式
  • Jenkins新安装的插件ThinBackup,如何恢复之前的备份
  • YOLOv5改进——普通卷积和C3模块更换为GhostConvV2卷积和C3GhostV2模块
  • GO网络编程(五):海量用户通信系统3:整体框架与C/S通信总体流程【重要】
  • 解决github每次pull push输入密码问题
  • opencascade鼠标拖拽框选功能
  • Java | Leetcode Java题解之第468题验证IP地址
  • Windows10的MinGW安装和VS Code配置C/C++编译环境
  • mermaid 图表相关
  • 408算法题leetcode--第29天
  • 如何让你的Mac右键菜单栏更加的丰富多样
  • Vue vben admin开源库中table组件tips
  • 数据结构进阶:二叉搜索树_C++