test6_2.txt

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

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

#define NEWFILE (O_WRONLY|O_CREAT|O_TRUNC)
#define SIZE 80

int write_buffer(int fd, char *buf, int count);

int main(void)
{
    	int outfile;
    	char filename[ ]={"test.dat"};
	char buffer[SIZE];

	if(outfile=open(filename, NEWFILE, 0640)==-1)
	{
    		printf("ERROR, OPEN FILE FAILED! \n");
    		exit(255);
	}

	while(!strcmp(buffer,"quit"))
	{
		gets(buffer);
		if(write_buffer(outfile, buffer, SIZE)==-1)
		{
        		printf("ERROR,WRITE FAILED: \n",sys_errlist[errno]);
        		exit(255);
		}
    	}
    	close(outfile);
    	return 0;
}


int write_buffer(int fd, char *buf, int count)
{
    	int i,n;
	char write_buf[SIZE];
	int write_offset=0;
    	for(i=0; i<count; ++i)
    	{
        	write_buf[write_offset++]=*buf++;
        	if(write_offset==SIZE)
        	{
            		write_offset=0;
            		n=write(fd, write_buf, sizeof(write_buf));

            		if(n!=SIZE)
                		return -1;
         	}
    	}
	return -1;
}

⌨️ 快捷键说明

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