0
点赞
收藏
分享

微信扫一扫

文件IO操作

书呆鱼 2022-02-21 阅读 85

head.h

#ifndef _HEAD_H_
#define _HEAD_H_

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#endif

01_open.c

#include "head.h"

int main(void)
{
	int fd = open("1.txt",O_RDWR | O_CREAT | O_TRUNC,0664);
	if(-1 == fd)
	{
		printf("fail to open.\n");
		return -1;
	}
	printf("fd = %d\n",fd);

	close(fd);
	
	return 0;
}

02_write.c

#include "head.h"

int main(void)
{
	int fd = open("1.txt",O_RDWR | O_CREAT | O_TRUNC,0664);
	if(-1 == fd)
	{
		printf("fail to open.\n");
		return -1;
	}

	char buff[] = {"hello world"};
	ssize_t ret = write(fd,buff,strlen(buff));
	printf("ret = %ld\n",ret);

	close(fd);
	
	return 0;
}

03_read.c

#include "head.h"

int main(void)
{
	int fd = open("1.txt",O_RDONLY);
	if(-1 == fd)
	{
		printf("fail to open.\n");
		return -1;
	}

	char buff[1024] = {"lllllllllllllllllllllllllll"};
	memset(buff,0,sizeof(buff));//将指定空间清成你想要的字符
	ssize_t ret = read(fd,buff,sizeof(buff));
	printf("ret = %ld\n",ret);
	printf("%s\n",buff);

	close(fd);
	
	return 0;
}

04_lseek.c

#include"head.h"

int main(int argc, const char *argv[])
{
	int fd = open("1.txt",O_RDWR | O_CREAT,0664);
	if(-1 == fd)
	{
		printf("fail to open.\n");
		return -1;
	}
    off_t offset = lseek(fd,3,SEEK_SET);
	printf("offset = %ld\n",offset);
	write(fd,"A",1);

	offset = lseek(fd,4,SEEK_CUR);
	printf("offset = %ld\n",offset);
	write(fd,"B",1);

	offset = lseek(fd,-2,SEEK_END);
	printf("offset = %ld\n",offset);
	write(fd,"C",1);

	offset = lseek(fd,0,SEEK_END);
	printf("len = %ld\n",offset);

	close(fd);
	
	return 0;
}

05_buffer.c

06_fileno.c

#include "head.h"

int main(int argc, const char *argv[])
{
	FILE *fp = fopen("1.txt","r+");
	int fd = fileno(fp);

	write(fd,"hello world",11);
	fclose(fp);
	return 0;
}

07_fdopen.c

#include "head.h"

int main(int argc, const char *argv[])
{
	int fd = open("1.txt",O_RDONLY);
	FILE * fp = fdopen(fd,"r");
	char buff[1024] = {0};
	fgets(buff,sizeof(buff),fp);
	printf("%s",buff);
	close(fd);
	
	return 0;
}

08_time.c

#include "head.h"


int main(int argc, const char *argv[])
{
	FILE *fp = fopen("1.txt","w+");
	time_t sec = 0;
	time(&sec);
//	printf("sec = %ld\n",sec);

	char *pt = ctime(&sec);
//	printf("pt = %s",pt);

	struct tm * ptime = localtime(&sec);
	printf("%d-%d-%d %d:%d:%d\n",ptime->tm_year+1900,ptime->tm_mon+1,ptime->tm_mday,ptime->tm_hour,ptime->tm_min,ptime->tm_sec);
	
	char buff[1024] = {0};
	fgets(buff,sizeof(buff),stdin);
	fputs(buff,fp);
	return 0;
}

09_log.c  

#include "head.h"

int main(int argc, const char *argv[])
{
	FILE *fp = fopen("log.txt","a");
	if(NULL == fp)
	{
		printf("fail to fopen.\n");
		return -1;
	}

	while(1)
	{
		char buff[1024] = {0};
		fgets(buff,sizeof(buff),stdin);
		buff[strlen(buff) - 1] = '\0';

		time_t sec;
		time(&sec);
		struct tm * pt = localtime(&sec);
		fprintf(fp,"%s %d-%d-%d %d:%d:%d\n",buff,pt->tm_year+1900,pt->tm_mon+1,pt->tm_mday,pt->tm_hour,pt->tm_min,pt->tm_sec);
		fflush(fp);
		if(strcmp(buff,"quit") == 0)
		{
			break;
		}
	}
	fclose(fp);
	return 0;
}
举报

相关推荐

IO文件操作

文件操作详解-IO

GoLang 文件IO操作

IO路-文件操作

【Java】文件操作和IO

【JavaEE】文件操作与IO

Python 文件 IO 操作详解

0 条评论