LinuxC编程之IO-通过lseek对文件进行读写

1.相关API

通过lseek对文件进行读写
1)open函数
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
2)read和write函数
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
3)lseek更改文件偏移位置
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
4)把字符串转化成数字类型
头文件:#include <stdlib.h>

strtol() 函数用来将字符串转换为长整型数(long),其原型为:
long int strtol (const char* str, char** endptr, int base);
【参数说明】str 为要转换的字符串,endstr 为第一个不能转换的字符的指针,base 为字符串 str 所采用的进制。
实现代码:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>          //errno
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>         //strtol()
#include <unistd.h>         //ssize_t

int main(int argc, char *argv[])
{
    size_t len;
    off_t offset;
    int fd, ap, j;
    char *buf;
    ssize_t numRead, numWritten;
    if(argc < 3 || strcmp(argv[1], "--help") == 0)
        printf("%s file {r<length>| R<length> | w<string>| s<offset>}...\n", argv[0]);
    fd = open(argv[1], O_RDWR| O_CREAT, S_IRUSR| S_IWUSR| S_IRGRP | S_IWGRP | S_IROTH| S_IWOTH);
    if(fd == -1)
    {
        //打开文件出错
        fprintf(stderr, "Open %s Error: %s\n", argv[1], strerror(errno));
        exit(1);
    }
    for (ap = 2; ap < argc; ++ap) {
        switch (argv[ap][0]){
            case 'r':   //在当前offset偏移处显示文件内容
            case 'R':
                len=strtol(&argv[ap][1], NULL, 0);
                buf = malloc(len);
                if(buf == NULL){
                    printf("malloc buf error\n");
                    exit(1);
                }
                numRead = read(fd, buf, len);
                if(numRead == -1){
                    printf("malloc buf error\n");
                    free(buf);
                    exit(1);
                }
                if(numRead == 0){
                    printf("%s: end-of-file\n", argv[ap]);
                } else{
                    printf("%s: ",argv[ap]);
                    for(j = 0; j< numRead; j++){
                        if (argv[ap][0]=='r'){
                            printf("%c", isprint((unsigned char)buf[j]) ? buf[j]: '?');
                        } else{
                            printf("%02x", (unsigned int) buf[j]);
                        }
                    }
                    printf("\n");
                }
                if(buf != NULL)
                    free(buf);
                break;
            case 'w':           //在当前文件偏移下写入内容
                numWritten = write(fd, &argv[ap][1], strlen(&argv[ap][1]));
                if(numWritten == -1){
                    printf("write buf error\n");
                    exit(1);
                }
                printf("%s: wrote %ld bytes\n", argv[ap], (long) numWritten);
                break;
            case 's':           //从文件头开始更改文件的偏移位置
                offset = strtol(&argv[ap][1], NULL, 0);
                //offset = getLong(&argv[ap][1], GN_ANY_BASE, argv[ap]);
                if(lseek(fd, offset, SEEK_SET) == -1){
                    printf("lseek offset error\n");
                    exit(1);
                }
                printf("%s: seek successed\n", argv[ap]);
                break;
            default:
                printf("Argument must start with [rRws]\n");
                break;
        }
    }//end for
    printf("file execute success\n");
}
运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值