fopen函数使用基础

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


#define MAX_SIZE 1024*8


unsigned long long GetTickCount()
{
	struct timespec ts;
	clock_gettime(CLOCK_MONOTONIC,&ts);
	//return(ts.tv_sec*1000+ts.tv_nsec/1000000);  /* 获得系统时间毫秒 */
	return(ts.tv_sec*1000*1000+ts.tv_nsec/1000);    /* 获得系统时间微秒 */
	//return(ts.tv_sec*1000*1000*1000+ts.tv_nsec);    /* 获得系统时间纳秒 */ 有问题
}


int main(int argc, char *argv[])
{
#if 0
	while(1)
	{
		sleep(1);
		printf("TTT------nsec[%llu]-----------\n", GetTickCount());
	}
#else
	int file_len;
	int len, n;
	char *ptr = NULL;
	char buf[MAX_SIZE] = {0};
	int fd;
	char file_name[] = {"/record/hd01/qh00013.dat"};
	
	fd = open(file_name, O_RDWR | O_CREAT);
	if(-1 == fd)
	{
		printf("TTT-----open fail \n");
		return -1;
	}


	len = sizeof(buf);
	printf("TTT----len[%d]-----\n", len);
	ptr = buf;	
	n   = len;
	while(0 < n)
	{
		memcpy(ptr, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 32);
		ptr += 32;
		n   -= 32;
	}

	file_len = 0;
	lseek(fd, 0, SEEK_SET);
	while(1)
	{
		n = write(fd, buf, len);
		if(n < 0)
		{
			printf("TTT----write error \n");
		}
		else
		{
			printf("TTT----write len[%d]----return[%d]  \n", len, n);
		}
		
		file_len += n;
		if(500*1024*1024 < file_len)
		{
			file_len = 0;
			lseek(fd, 0, SEEK_SET);
		}		
		usleep(10*1000);
	}
#endif	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/tanhuifang520/article/details/50983553