test6_4.txt

来自「嵌入式c源码」· 文本 代码 · 共 45 行

TXT
45
字号
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>

#define NEWFILE (O_WRONLY|O_CREAT|O_TRUNC)

int main(void)
{
    	char buf1[ ]={"abcdefghij"};
    	char buf2[ ]={"1234567890"};
	int fd;
	int length;

	if(fd=open("test.hole", NEWFILE, 0600)==-1)
	{
    		printf("ERROR, OPEN WRITE FILE FAILED: \n", sys_errlist[errno]);
    		exit(255);
	}

	length=strlen(buf1);
    	if(write(fd, buf1,length)!=length)
    	{
    		printf("ERROR, OPEN WRITE FILE FAILED: \n", sys_errlist[errno]);
    		exit(255);
	}

    	if(lseek(fd, 50, SEEK_SET)==-1)
    	{
    		printf("ERROR, LSEEK FAILED: \n", sys_errlist[errno]);
        	exit(255);
    	}

	length=strlen(buf2);
    	if(write(fd, buf2,length)!=length)
    	{
    		printf("ERROR, OPEN WRITE FILE FAILED: \n", sys_errlist[errno]);
    		exit(255);
	}

	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?