今天介绍两个函数用以代替库函数atoi()、atol()和strtol()函数,原因是它能提供比库函数更好的错误检查机制。

int getInt(const char *arg, int flags, const char *name);

long getLong(const char *arg, int flags, const char *name);

arg指向待转的数字字符串,flags有固定的几个宏值,代表待转换数字字符串的进制情况,name指向待转字符的说明字符串,用于发生错误时添加到错误消息中。

flags可以用或的形式指定多个值,它们可以控制函数转换的进制、范围等。

flags可取以下值:GN_NONNEG、GN_GT_0 、GN_ANY_BASE、 GN_BASE_8、 GN_BASE_16

其头文件命名为get_num.h:

/*

FUNC_NAME: get_num.h

FUNC_DESC: Header file for get_num.c.

*/

#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:

/*

FUNC_NAME: get_num.h

FUNC_DESC: Functions to process numeric command-line arguments.

*/

#include

#include

#include

#include

#include

#include "get_num.h"

/* Print a diagnostic message that contains a function name ('fname'),

the value of a command-line argument ('arg'), the name of that

command-line argument ('name'), and a diagnostic error message ('msg'). */

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", msg);

if (arg != NULL && *arg != '\0')

fprintf(stderr, " offending text: %s\n", arg);

exit(EXIT_FAILURE);

}

/* Convert a numeric command-line argument ('arg') into a long integer,

returned as the function result. 'flags' is a bit mask of flags controlling

how the conversion is done and what diagnostic checks are performed on the

numeric result; see get_num.h for details.

'fname' is the name of our caller, and 'name' is the name associated with

the command-line argument 'arg'. 'fname' and 'name' are used to print a

diagnostic message in case an error is detected when processing 'arg'. */

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;

}

/* Convert a numeric command-line argument string to a long integer. See the

comments for getNum() for a description of the arguments to this function. */

long

getLong(const char *arg, int flags, const char *name)

{

return getNum("getLong", arg, flags, name);

}

/* Convert a numeric command-line argument string to an integer. See the

comments for getNum() for a description of the arguments to this function. */

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;

}

linux atoi,atoi()的替代办法相关推荐

  1. 远程连接Linux服务器无法连接解决办法

    1.查看SSH是否安装(检查是否装了SSH包) 输入命令:rpm -qa | grep ssh 如下如所示系统已经默认安装了SSH: 远程连接Linux服务器无法连接解决办法 若没有安装,则输入 yu ...

  2. 10 款你不知道的 Linux 环境下的替代工具

    作者:JackTian 来源:公众号「杰哥的IT之旅」 ID:Jake_Internet 转载请联系授权(微信ID:Hc220088) 原文地址:10 款你不知道的 Linux 环境下的替代工具! 大 ...

  3. windows文件上传到linux平台乱码的解决办法

    windows文件上传到linux平台乱码的解决办法 1.首先在windows上,使用ConvertZ工具,把文件名称(不是文件内容)转码,例如GBK换成Unicode 简体 2.然后上传到linux ...

  4. RedFlag Linux忘记root密码解决办法

    转于lee的http://hi.baidu.com/maozilee/item/12a62a76f371df2bd7a89c5d RedFlag Linux忘记root密码解决办法 Linux忘记ro ...

  5. Linux下的AudoCAD替代软件

    分享一下我老师大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow Linux下的Au ...

  6. linux 检查僵死进程,Linux下杀僵尸进程办法

    Linux认证辅导:Linux下杀僵尸进程办法 为了方便广大考生更好的复习,小编编辑整理提供了Linux认证:Linux下杀僵尸进程办法,以供各位考生考试复习参考,希望对考生复习有所帮助. 1) 检查 ...

  7. 使用SSH客户端远程登录Linux主机(可替代samba、ftp服务)

    使用SSH客户端远程登录Linux主机(可替代samba.ftp服务) Linux系统起初就是为多用户而产生的,可以允许多个用户同时登录linux主机各自进行操作,如图1所示:   图1 SSH(Se ...

  8. linux文件系统变成只读,Linux文件系统变成只读解决办法

    this.p={ m:2, b:2, loftPermalink:'', id:'fks_0950650870810800660930850870950850840830690920860850740 ...

  9. linux c atoi strtol 区别

    atoi和strtol函数均是把字符串转换成整数,两者的不同点主要是: 1,atoi的返回值无法区分是正常的返回还是错误的返回,如: int val; val = atoi("abc&quo ...

  10. linux中atoi函数的实现 值得借鉴,【转】atoi()函数的实现

    atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...

最新文章

  1. 转帖:硬盘生产全过程(图)
  2. 【PHPExcel】生成Excel文件
  3. 自然语言处理好的 实体分词 及BERT
  4. linux下VNC配置详解
  5. OpenCV跟踪支持的实例(附完整源代码)
  6. JQuery UI 拖拽排序
  7. Android之调用微信登陆、分享、支付
  8. sql 中on和where的区别
  9. iOS开发之网络编程--6、NSURLSessionConfiguration笔记
  10. 一步一步学Linq to sql(十):分层构架的例子
  11. 什么是4G工业智能网关?与DTU有什么区别
  12. Android 强制设置应用横屏或竖屏
  13. 微信公众平台开发入门教程[2020版]
  14. 应用|5G时代10大应用场景!
  15. 1.Windows环境配置
  16. eap协议 c语言,CCNP无线技术知识点-EAP和EAPOL协议报文详解
  17. web2.0涉及的一些技术摘要
  18. Arcgis 计算每个网格点内线段长度
  19. Android 上手机跟机顶盒应用开发的区别
  20. 任天堂官宣塞尔达传说新作:为新产品护航?

热门文章

  1. 封装判断一个字符的后缀名和前缀的方法
  2. java中的UDP总结
  3. 181004有道扇贝每日一句
  4. atitit 项目管理 package 模块管理 包管理 依赖管理 maven attilax总结.docx
  5. Atitit v2 ajax 最佳实践规范 标准化流程attilax总结 r34
  6. Atitit 判断判断一张图片是否包含另一张小图片
  7. atitit.atiLinq v2新特性attilax大总结 q326
  8. paip.mysql 全文索引查询空白解决
  9. paip.输入法编程---词频顺序order by py
  10. (转)专访Palantir创始人:如何接二连三创出独角兽公司?