天天看点

学习日志 上课190802

Linux 文件编程

写入3个hello 并打印

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

int main()
{
    char read_buf [6];
    char write_buf [6] = "hello";
    int i;
    int bytes_read;
    int bytes_write;
    int fd;

    fd = open("hello.txt",O_RDWR | O_CREAT,0644);

    if (fd == -1)
    {
        perror("open error!");
        exit(-1);
    }

    for (i = 0; i < 3; i++)
    {
        bytes_write = write(fd,write_buf,5);

        if (bytes_write == -1)
        {
            perror("write error!");
            close(fd);
            exit(-1);
        }
        else
        {
            write(fd,"\n",1);
        }
    }
   
    lseek(fd,0,SEEK_SET);

    for (i = 0; i < 3; i++)
    {
        bytes_read = read(fd,write_buf,6);
        if (bytes_read == -1)
        {
            perror("read error!");
            close(fd);
            exit(-1);
        }
        write(1,write_buf,6);
    }

//    read(fd,write_buf,18);
//    write(1,write_buf,18);

    close(fd);


    return 0;
}
           

文件的复制粘贴

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

#define BUFFER_SIZE 1024

int main(int argc, char ** argv)
{
    int from_fd,to_fd;
    int bytes_read;
    int bytes_write;
    char buf[BUFFER_SIZE];
    char *ptr;

    if (argc != 3)
    {
        fprintf(stderr,"usage: %s need from and to file!\n");
        exit(0);
    }

    if ((from_fd = open(argv[1],O_RDONLY)) == -1)
    {
        fprintf(stderr,"open %s error : %s\n",argv[1],strerror(errno));
        exit(-1);
    }

    if ((from_fd = open(argv[2],O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR)) == -1)
    {
        fprintf(stderr,"open %s error : %s\n",argv[1],strerror(errno));
        exit(-1);
    }

    while (bytes_read = read(from_fd,buf,BUFFER_SIZE))
    {
        if (bytes_read == -1)
        {
            break;
        }
        else if(bytes_read > 0)
        {
            ptr = buf;
            while (bytes_write = write(to_fd,ptr,bytes_read))
            {
                if (bytes_write == -1)
                {
                    break;
                }
                else if (bytes_write == bytes_read)
                {
                    break;
                }
                else if (bytes_write > 0)
                {
                    bytes_read -= bytes_write;
                    ptr += bytes_write;
                }
            }
            if (bytes_write == -1)
            {
                break;
            }

        }
    }

    return 0;
}
           

利用lseek来计算文件长度

lseek(fd, 0, SEEK_END);
           

需要查看代码

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define MAX 100

int read_line(int fd, char *buf, int count)
{
	int i;
	char ch;

	for(i = 0; i < count; i++)
	{
        if((read(fd,&ch,1)) < 0)
		{
			perror("read error!\n");
			exit(1);
		}

		if(ch == '\n')
		{
             buf[i] = '\0';
			 return i;
		}

		buf[i] = ch;
	}

	buf[i - 1] = '\0';
	
	return count;
}

int main()
{
	int i;
	int fd;
	int w_count;
	int r_count;
	char filename[MAX];

	char r_buf[MAX];
	char w_buf[MAX];

	printf("Please input filename:\n");
	scanf("%s",filename);

	if((fd = open(filename, O_CREAT | O_RDWR, 0755)) < 0)
	{
		perror("open error!\n");
		exit(1);
	}

	
    for(i = 0; i < 3; i++)
	{
        memset(w_buf,0,sizeof(w_buf));
		scanf("%s",w_buf);
		
		if((w_count = write(fd,w_buf,strlen(w_buf))) < 0)
		{
			perror("write error!\n");
			exit(1);
		}
		else
		{
			write(fd,"\n",1);
		}
	}
    
    lseek(fd,0,SEEK_SET);
#if 0
	if((r_count = read(fd,r_buf,sizeof(r_buf))) < 0)
	{
		perror("read error!\n");
		exit(1);
	}
	else
	{
		r_buf[w_count * 3 + 3] = '\0';
		printf("read data: %s\n",r_buf);
	}
#endif

	for(i = 0; i < 3; i++)
	{
        memset(r_buf,0,sizeof(r_buf));
		read_line(fd,r_buf,100);
		printf("read data: %s\n",r_buf);
	}

	close(fd);

	return 0;
}