linux 环境下文件io

 /*********************************************************************************
 *      Copyright:  (C) 2018 ligang<2820724771@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  file.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(01/31/2018)
 *         Author:  ligang <2820724771@qq.com>
 *      ChangeLog:  1, Release initial version on "01/31/2018 02:46:39 AM"
 *                 
 ********************************************************************************/
//1.先创建一个文本文件叫test.txt,里面写一段字符串,至少有10行;
//构想:printf("hello%s",string)把字符串内容打印到终端,那么用snprintf写出hello1,hello2,hello3....到字符串中
//int printf(const char *format, ...);    int snprintf(char *str, size_t size, const char *format, ...);
//用write写时用strlen(),写时buf写入的大小也注意。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>

int main(int argc,char *argv[])
{
    FILE    *fp;
    int fd;
    char buf[64];
    int i=0;


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

    if(fd<0)
    {
        printf("open file is faliure! %s",strerror(errno));
    }

    for(;i<10;i++)
    {
        snprintf(buf,sizeof(buf),"hello world%d\n",i);
        write(fd,buf,strlen(buf));
    }
  
    close(fd);
    return 0;
}

编译运行
[ligang@centos6 apue_test]$ gcc file.c 
[ligang@centos6 apue_test]$ ./a.out
[ligang@centos6 apue_test]$ cat hello.txt 
hello world0
hello world1
hello world2
hello world3
hello world4
hello world5
hello world6
hello world7
hello world8
hello world9

2.把test.txt里所有的内容打印到标准输出;
//构想把test.txt里面内容一行一行地打印出来  用fgets 
//char *fgets(char *s, int size, FILE *stream);我们需要文件流fp,所以用fdopen把fd转化为fp
//但此时注意文件偏移量,写了之后文件偏移量默认是文件头,没有设置文件偏移量会导致内容读不出。
 #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>

int main(int argc,char *argv[])
{
    FILE    *fp;
    int fd;
    char buf[64];
    int i=0;


    fd = open("hello.txt",O_RDWR|O_CREAT|O_TRUNC,0644);
    if(fd<0)
    {
        printf("open file is faliure! %s",strerror(errno));
    }

	
    for(;i<10;i++)
    {
        snprintf(buf,sizeof(buf),"hello world%d\n",i);
        write(fd,buf,strlen(buf));
    }
	
	
    lseek(fd,0,SEEK_SET); 
    fp = fdopen(fd,"r");

    while(fgets(buf,sizeof(buf),fp)!=NULL)
    {
        printf("%s",buf);    
    }

    close(fd);
    return 0;
}
编译运行
[ligang@centos6 apue_test]$ gcc file.c 
[ligang@centos6 apue_test]$ ./a.out 
hello world0
hello world1	
hello world2
hello world3
hello world4
hello world5
hello world6
hello world7
hello world8
hello world9
[ligang@centos6 apue_test]$ cat hello.txt 
hello world0
hello world1
hello world2
hello world3
hello world4
hello world5
hello world6
hello world7
hello world8
hello world9

3,再把这个文本文件拷贝一份,重命名为test.log
//打开hello.txt,创建文件test.log,再把内容写到test.log去  读时 用read函数实现
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>

int main(int argc,char *argv[])
{
    FILE    *fp;
    int fd,fd1;
    char buf[64];
    int i=0;
    int  nbytes;

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

    if(fd<0)
    {
        printf("open file is faliure! %s",strerror(errno));
        return -1;
    }

    for(;i<10;i++)
    {
        snprintf(buf,sizeof(buf),"hello world%d\n",i);
        write(fd,buf,strlen(buf));
    }


    memset(buf,0,sizeof(buf));
    lseek(fd,0,SEEK_SET); 
    fp = fdopen(fd,"r");

    while(fgets(buf,sizeof(buf),fp)!=NULL)
    {
        printf("%s",buf);    
    }
    

    lseek(fd,0,SEEK_SET);//设置文件偏移量
    memset(buf,0,sizeof(buf));
    fd1 = open("test.log",O_RDWR | O_CREAT | O_TRUNC,0644);
    if(fd1<0)
    {
        printf("open file faliure %s",strerror(errno));
        return -1
    
    }
    while((nbytes = read(fd,buf,sizeof(buf)))!=0)
    {
        write(fd1,buf,nbytes);
    }


    close(fd1);
    close(fd);
    return 0;
}
ligang@centos6 apue_test]$ gcc file.c 
[ligang@centos6 apue_test]$ ./a.out
hello world0
hello world1
hello world2
hello world3
hello world4
hello world5
hello world6
hello world7
hello world8
hello world9
[ligang@centos6 apue_test]$ cat test.log 
hello world0
hello world1
hello world2
hello world3
hello world4
hello world5
hello world6
hello world7
hello world8
hello world9


完整函数实现,每个功能抽象成一个函数
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>

#define MAX_SIZE 128

int file_open_write(int fd,int line);
int file_print(int fd);
int file_copy(int fd);

int main(int argc,char *argv[])
{    
    int fd;

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

    if(fd<0)
    {
        printf("open file is faliure! %s",strerror(errno));
        return -1;
    }

    
        file_open_write(fd,10);
   
    file_print(fd);

        file_copy(fd);

    close(fd);
    return 0;
}


int file_open_write(int fd,int line)
{
        int i=0;
    char buf[MAX_SIZE];

    for(;i<line;i++)
    {
                snprintf(buf,sizeof(buf),"hello world%d\n",i);
                write(fd,buf,strlen(buf));
    }
        return 0;
}

int file_print(int fd)
{
        FILE    *fp;
        char buf[MAX_SIZE];

        memset(buf,0,sizeof(buf));
    lseek(fd,0,SEEK_SET); 
    fp = fdopen(fd,"r");

    while(fgets(buf,sizeof(buf),fp)!=NULL)
    {
        printf("%s",buf);    
    }
        return 0;
}

int file_copy(int fd)
{
        int     fd1;
        int nbytes;
        char    buf[MAX_SIZE];

        fd1 = open("test.log",O_RDWR | O_CREAT | O_TRUNC,0644);
        if(fd1 < 0)
        {
                printf("open file is faliure %s",strerror(errno));
                return -1;
        }

        memset(buf,0,sizeof(buf));
        lseek(fd,0,SEEK_SET);
        while((nbytes=read(fd,buf,sizeof(buf))) > 0)
        {
                write(fd1,buf,nbytes);
        }

        close(fd1);
        return 0;
}

编译运行
[ligang@centos6 c_study]$ gcc file.c 
[ligang@centos6 c_study]$ ./a.out 
hello world0
hello world1
hello world2
hello world3
hello world4
hello world5
hello world6
hello world7
hello world8
hello world9
[ligang@centos6 c_study]$ cat test.log 
hello world0
hello world1
hello world2
hello world3
hello world4
hello world5
hello world6
hello world7
hello world8
hello world9
[ligang@centos6 c_study]$ cat hello.txt 
hello world0
hello world1
hello world2
hello world3
hello world4
hello world5
hello world6
hello world7
hello world8
hello world9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值