LinuxC程式設計之IO-通過lseek對檔案進行讀寫
阿新 • • 發佈:2019-01-01
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"); }