⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test6_2.txt

📁 Linux下的C语言编程
💻 TXT
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -