Unix系统编程()open,read,write和lseek的综合练习
需求:程序的第一个命令行参数为将要打开的文件名称,余下的参数则指定了文件上执行的输入输出操作。每个表示操作的参数都以一个字母开头,紧跟以相关值(中间无空格分隔)。
soffet:从文件开始检索到offset字节位置
rlength:在文件当前偏移量处,从文件中读取length字节数据,并以文本形式显式
Rlength:在当前文件偏移量处,从文件中读取length字节数据,并以十六进制形式显式
wstr:在当前文件偏移量处,由文件写入由str指定的字符串
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include "get_num.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
size_t len;
off_t offset;
int fd, ap, j;
char *buf;
ssize_t numRead, numWritten; /* usage */
if(argc < 3 || strcmp(argv[1], "--help") == 0)
printf("%s file {r<length> | R<length> | w<string> | s<offset>} ...\n",
argv[0]); /* open or create file */
fd = open(argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
S_IROTH | S_IWOTH); /* is system call success */
if(fd == -1)
printf("open file error\n"); /* biz code */
for(ap = 2; ap < argc; ap++) {
switch(argv[ap][0]) {
case 'r': /* Display bytes at current offset, as text */
case 'R': /* Display bytes at current offset, in hex */
len = getLong(&argv[ap][1], GN_ANY_BASE, argv[ap]); /* alloc buffer */
buf = malloc(len); /* is alloc success */
if(buf == NULL)
printf("malloc error\n"); numRead = read(fd, buf, len);
if(numRead == -1)
/* read fail */
printf("read\n"); /* end of file */
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("%O2x ", (unsigned int) buf[j]);
printf("\n");
} /* free memory */
free(buf);
break;
case 'w': /* Write string at current offset */
numWritten = write(fd, &argv[ap][1], strlen(&argv[ap][1]));
if(numWritten == -1)
printf("write error\n");
printf("%s: wrote %ld bytes\n", argv[ap], (long) numWritten);
break;
case 's':
offset = getLong(&argv[ap][1], GN_ANY_BASE, argv[ap]);
if(lseek(fd, offset, SEEK_SET) == -1)
printf("lseek error!\n");
printf("%s: seek successed\n", argv[ap]);
break;
default:
printf("Argument must start with [rRws]: %s\n", argv[ap]);
}
} exit(0);
}
get_num.h
#ifndef GET_NUM_H
#define GET_NUM_H #define GN_NONNEG 01 /* Value must be >= 0 */
#define GN_GT_0 02 /* Value must be > 0 */ /* By default, integers are decimal */
#define GN_ANY_BASE 0100 /* Can use any base - like strtol(3) */
#define GN_BASE_8 0200 /* Value is expressed in octal */
#define GN_BASE_16 0400 /* Value is expressed in hexadecimal */ long getLong(const char *arg, int flags, const char *name); int getInt(const char *arg, int flags, const char *name); #endif
get_num.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include "get_num.h" static void gnFail(const char *fname, const char *msg, const char *arg,
const char *name) {
fprintf(stderr, "%s error", fname); if (name != NULL)
fprintf(stderr, " (in %s)", name); fprintf(stderr, ": %s\n", name); if(arg != NULL && *arg != '\0')
fprintf(stderr, " offending text: %s\n", arg); exit(EXIT_FAILURE);
} static long getNum(const char *fname, const char *arg, int flags,
const char *name)
{
long res;
char *endptr;
int base; if(arg == NULL || *arg == '\0')
gnFail(fname, "null or empty string", arg, name); base = (flags & GN_ANY_BASE) ? 0 : (flags & GN_BASE_8) ? 8 :
(flags & GN_BASE_16) ? 16 : 10; errno = 0; res = strtol(arg, &endptr, base); if(errno != 0)
gnFail(fname, "strtol() failed", arg, name); if(*endptr != '\0')
gnFail(fname, "nonnumeric characters", arg, name); if((flags & GN_NONNEG) && res < 0)
gnFail(fname, "negative value not allowed", arg, name); if((flags & GN_GT_0) && res <= 0)
gnFail(fname, "value must be > 0", arg, name); return res;
} long getLong(const char *arg, int flags, const char *name)
{
return getNum("getLong", arg, flags, name);
} int getInt(const char *arg, int flags, const char *name)
{
long res; res = getNum("getInt", arg, flags, name);
if(res > INT_MAX || res < INT_MIN)
gnFail("getInt", "integer out of range", arg, name); return (int) res;
}
Unix系统编程()open,read,write和lseek的综合练习的更多相关文章
- linux系统编程:read,write与lseek的综合应用
这个实例根据命令行参数进行相应的读学操作: 用法: usage:./io file {r<length>|R<length>|w<string>|s<offs ...
- 《Linux/Unix系统编程手册》读书笔记8 (文件I/O缓冲)
<Linux/Unix系统编程手册>读书笔记 目录 第13章 这章主要将了关于文件I/O的缓冲. 系统I/O调用(即内核)和C语言标准库I/O函数(即stdio函数)在对磁盘进行操作的时候 ...
- 《Linux/Unix系统编程手册》读书笔记1
<Linux/Unix系统编程手册>读书笔记 目录 最近这一个月在看<Linux/Unix系统编程手册>,在学习关于Linux的系统编程.之前学习Linux的时候就打算写关于L ...
- 《Linux/Unix系统编程手册》读书笔记2
<Linux/Unix系统编程手册>读书笔记 目录 第5章: 主要介绍了文件I/O更深入的一些内容. 原子操作,将一个系统调用所要完成的所有动作作为一个不可中断的操作,一次性执行:这样可以 ...
- 《Linux/Unix系统编程手册》读书笔记 目录
<Linux/Unix系统编程手册>读书笔记1 (创建于4月3日,最后更新4月7日) <Linux/Unix系统编程手册>读书笔记2 (创建于4月9日,最后更新4月10日) ...
- 《Linux/Unix系统编程手册》读书笔记9(文件属性)
<Linux/Unix系统编程手册>读书笔记 目录 在Linux里,万物皆文件.所以文件系统在Linux系统占有重要的地位.本文主要介绍的是文件的属性,只是稍微提及一下文件系统,日后如果有 ...
- 《Linux/Unix系统编程手册》读书笔记7 (/proc文件的简介和运用)
<Linux/Unix系统编程手册>读书笔记 目录 第11章 这章主要讲了关于Linux和UNIX的系统资源的限制. 关于限制都存在一个最小值,这些最小值为<limits.h> ...
- 《Linux/Unix系统编程手册》读书笔记6
<Linux/Unix系统编程手册>读书笔记 目录 第9章 这章主要讲了一堆关于进程的ID.实际用户(组)ID.有效用户(组)ID.保存设置用户(组)ID.文件系统用户(组)ID.和辅助组 ...
- 《Linux/Unix系统编程手册》读书笔记5
<Linux/Unix系统编程手册>读书笔记 目录 第8章 本章讲了用户和组,还有记录用户的密码文件/etc/passwd,shadow密码文件/etc/shadow还有组文件/etc/g ...
- 《Linux/Unix系统编程手册》读书笔记4
<Linux/Unix系统编程手册>读书笔记 目录 第7章: 内存分配 通过增加堆的大小分配内存,通过提升program break位置的高度来分配内存. 基本学过C语言的都用过mallo ...
随机推荐
- 【实践】用for-in 循环实现三联联动
之前用jq 做过一次三联联动以及四联联动 现在为了更好地了解对象用js的原生方式做了一次 *本节要点方法: obj.selectedIndex 获取下拉列表选中的option 的索引 obj.o ...
- 2017.12.27 sqlSessionFactory和sqlSession(to be continued)
参考来自:<深入浅出MyBatis技术原理与实践-第6章 > 1.SqlSessionFactory SqlSessionFactory是一个接口,最重要的功能是提供SqlSession. ...
- MAC COCOA一个简单的多线程程序
功能: 实现多线程:2个线程同一时候工作,一个用时间计数器.一个用来信息打印 STEP1 XCODE ->New Application ->Cocoa中的Command Line 自己主 ...
- 矩阵LU分解分块算法实现
本文主要描述实现LU分解算法过程中遇到的问题及解决方案,并给出了全部源代码. 1. 什么是LU分解? 矩阵的LU分解源于线性方程组的高斯消元过程.对于一个含有N个变量的N个线性方程组,总可以用高斯消去 ...
- hdu 4021 24 Puzzle ( 逆序数判断是否可解 )
24 Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total ...
- Loadrunner定时执行脚本
# -*- coding: utf-8 -*- import timeimport os #格式为小时,分钟,脚本名称(包括盘符,最好是放在根目录下)#需要把LoadRunner安装路径的Bin加入系 ...
- Java学习笔记1、常用dos命令
cd 改变当前目录 sys 制作DOS系统盘 copy 拷贝文件 del 删除文件 deltree 删除目录树 dir 列文件名 diskcopy 制磁盘 edit 文本编辑 format ...
- SQLiteDatabase 源码
/** * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Vers ...
- 一个简单的python爬虫(转)
# -*- coding: utf-8 -*- #--------------------------------------- # 程序:百度贴吧爬虫 # 版本:0.1 # 作者:why # 日期: ...
- 带 IK 分词器的 Luke 和 搜索应用服务器solr
首先在网上查了一下: Solr Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索 ...