c读写文件实例

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

int main(int argc,char *argv[]){
    //读取文件路径
    char *filepath = argv[1];
    //保存文件路径
    char *savepath = argv[2];
    int fd,savefd,size;
    char buffer[1024];
    struct stat fstat;
    mode_t mode;

    //打开文件
    fd = open(filepath,O_RDWR);
    if((stat(filepath,&fstat)) < 0){
        perror("run fstat error");
    }
    //判断文件类型
    mode = fstat.st_mode;
    if(S_ISREG(mode)){
        printf("Type:regular file/n");
    }else if(S_ISDIR(mode)){
        printf("Type:direcory/n");
        return 0;
    }
    if(fd != -1){
        //在文件不存在新建时生效,使用8进制设置新建文件的权限
        savefd = open(savepath,O_RDWR|O_CREAT,0777);
        //读取文件内容
        while((size=read(fd,buffer,sizeof(buffer))) > 0){
            printf("%s",buffer);
            //写文件
            if(savefd != -1){
                //注意:读取多少,则写入多少,此处为size
                if((write(savefd,buffer,size)) == -1){
                    perror("Write error!");
                }
            }
        }
    }
    exit(0);
}                                                                    

/ 另一个文件

/*
**功能:文件读写实例
*/
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/file.h>
#include<string.h>
#include "lockset.h"
void lock_set(int fd,int type);
int main(int argc,char *argv[]){
 
 char *input = argv[1];
 char *filepath = argv[2];

 //注意一定需要先清零,否则读取的时候就会多读出无效的字符
 char rdbuff[100] = {0};
 
 printf("input:%s/n",input);
 printf("filepath:%s/n",filepath);
 int opfd,wrsize,rdsize,len;
 opfd = open(filepath,O_RDWR|O_CREAT,0777);
 if(opfd == -1){
  perror("Open Error!/n");
  exit(1);
 }
 //加上写入锁
 lock_set(opfd,F_WRLCK);
 if((wrsize = write(opfd,input,strlen(input))) == strlen(input)){
  printf("Write Success!/n");
 }
        //等待输入字符,起到暂停的作用
 getchar();

 //解锁
 lock_set(opfd,F_UNLCK);
 getchar();

 //加上读取锁
 lock_set(opfd,F_RDLCK);
 lseek(opfd,0,SEEK_SET);
 //当读取的rdsize == 0时表示读取完,-1表示读取失败
 if((rdsize = read(opfd,rdbuff,sizeof(rdbuff))) > 0){
  printf("Read:%s/n",rdbuff);
 }else{
  printf("Read Error!/n");
 }
 //解锁
 lock_set(opfd,F_UNLCK);
 getchar();
 close(opfd);
 exit(0);
}


void lock_set(int fd,int type){

 struct flock lock;
 lock.l_whence = SEEK_SET;
 lock.l_start = 0;
 lock.l_len = 0;
 while(1){
  lock.l_type = type;
  if((fcntl(fd,F_SETLK,&lock)) ==0){
   if(lock.l_type == F_RDLCK){
    printf("read lock set by %d/n",getpid());
   }else if(lock.l_type == F_WRLCK){
    printf("write lock set by %d/n",getpid());
   }else if(lock.l_type == F_UNLCK){
    printf("release lock set by %d/n",getpid());
   }
   return;
  }
  fcntl(fd,F_GETLK,&lock);
  if(lock.l_type != F_UNLCK){
   if(lock.l_type == F_RDLCK){
    printf("Read lock already set by %d/n",lock.l_pid);
   }else if(lock.l_type == F_WRLCK){
    printf("Write lock already set by %d/n",lock.l_pid);
   }
   getchar();
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值