ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C语言标准库(1)

2021-04-22 20:01:05  阅读:199  来源: 互联网

标签:opening locale errno Value C语言 标准 file Error


C语言标准库(1)

一、引入

C 标准库是一组 C 内置函数、常量和头文件,比如 <stdio.h>、<stdlib.h>、<math.h>,等等。这个标准库可以作为 C 程序员的参考手册。

二、常用C标准库

1.<assert.h>

(1) 简介

C 标准库的 assert.h头文件提供了一个名为 assert 的宏,它可用于验证程序做出的假设,并在假设为假时输出诊断消息。

已定义的宏 assert 指向另一个宏 NDEBUG,宏 NDEBUG 不是 <assert.h> 的一部分。如果已在引用 <assert.h> 的源文件中定义 NDEBUG 为宏名称,则 assert 宏的定义如下:

#define assert(ignore) ((void)0)

(2) 常用函数

其中只有唯一一个函数

void assert( 
   int expression 
);

这实际上是一个宏,不是一个函数,可用于在 C 程序中添加诊断。

i.参数

expression – 这可以是一个变量或任何 C 表达式。如果 expression 为 TRUE,assert() 不执行任何动作。如果 expression 为 FALSE,assert() 会在标准错误 stderr 上显示错误消息,并中止程序执行。

ii.返回值

这个宏不返回任何值。

2.<ctype.h>

(1) 简介

C 标准库的 ctype.h 头文件提供了一些函数,可用于测试和映射字符。

这些函数接受 int 作为参数,它的值必须是 EOF 或表示为一个无符号字符。

如果参数 c 满足描述的条件,则这些函数返回非零(true)。如果参数 c 不满足描述的条件,则这些函数返回零。

(2) 函数举例

a. isalnum()
template<Class CharType>
  bool isalnum(
     CharType _Ch,
     const locale& _Loc   
  )

该函数检查所传的字符是否是字母和数字。

i.参数

_Ch --这是要检查的字符。

_Loc --包含字母数字元素的区域设置进行测试。

ii.返回值

如果 _Ch 是一个数字或一个字母,则该函数返回非零值,否则返回 0。

iii.实例
// locale_isalnum.cpp
// compile with: /EHsc
#include <locale>
#include <iostream>

using namespace std;

int main( )   
{
   locale loc ( "German_Germany" );
   bool result1 = isalnum ( 'L', loc);
   bool result2 = isalnum ( '@', loc);
   bool result3 = isalnum ( '3', loc);

   if ( result1 )
      cout << "The character 'L' in the locale is "
           << "alphanumeric." << endl;
   else
      cout << "The character 'L' in the locale is "
           << " not alphanumeric." << endl;

   if ( result2 )
      cout << "The character '@' in the locale is "
           << "alphanumeric." << endl;
   else
      cout << "The character '@' in the locale is "
           << " not alphanumeric." << endl;

   if ( result3 )
      cout << "The character '3' in the locale is "
           << "alphanumeric." << endl;
   else
      cout << "The character '3' in the locale is "
           << " not alphanumeric." << endl;
}

输出结果:

The character 'L' in the locale is alphanumeric. The character '@' in the locale is  not alphanumeric. The character '3' in the locale is alphanumeric.
b. isalpha()

template<Class CharType>
   bool isalpha(
      CharType _Ch,
      const locale& _Loc
      )

该函数检查所传的字符是否是字母。

i.参数

_Ch --这是要检查的字符。

_Loc --包含字母数字元素的区域设置进行测试。

ii.返回值

如果 _Ch 是一个字母,则该函数返回非零值,否则返回 0。

iii.实例
// locale_isalpha.cpp
// compile with: /EHsc
#include <locale>
#include <iostream>

using namespace std;

int main( )   
{
   locale loc ( "German_Germany" );
   bool result1 = isalpha ( 'L', loc);
   bool result2 = isalpha ( '@', loc);
   bool result3 = isalpha ( '3', loc);

   if ( result1 )
      cout << "The character 'L' in the locale is "
           << "alphabetic." << endl;
   else
      cout << "The character 'L' in the locale is "
           << " not alphabetic." << endl;

   if ( result2 )
      cout << "The character '@' in the locale is "
           << "alphabetic." << endl;
   else
      cout << "The character '@' in the locale is "
           << " not alphabetic." << endl;

   if ( result3 )
      cout << "The character '3' in the locale is "
           << "alphabetic." << endl;
   else
      cout << "The character '3' in the locale is "
           << " not alphabetic." << endl;
}

输出结果:

The character 'L' in the locale is alphabetic.
The character '@' in the locale is  not alphabetic.
The character '3' in the locale is  not alphabetic.
c. iscntrl()
template<Class CharType>
   bool iscntrl(
      CharType _Ch, 
      const locale& _Loc
   )

该函数检查所传的字符是否是控制字符。

根据标准 ASCII 字符集,控制字符的 ASCII 编码介于 0x00 (NUL) 和 0x1f (US) 之间,以及 0x7f (DEL),某些平台的特定编译器实现还可以在扩展字符集(0x7f 以上)中定义额外的控制字符。

i.参数

_Ch --这是要检查的字符。

_Loc --包含字母数字元素的区域设置进行测试。

ii.返回值

如果 _Ch 是一个控制字符,则该函数返回非零值,否则返回 0。

iii.实例
// locale_iscntrl.cpp
// compile with: /EHsc
#include <locale>
#include <iostream>

using namespace std;

int main( )   
{
   locale loc ( "German_Germany" );
   bool result1 = iscntrl ( 'L', loc );
   bool result2 = iscntrl ( '\n', loc );
   bool result3 = iscntrl ( '\t', loc );

   if ( result1 )
      cout << "The character 'L' in the locale is "
           << "a control character." << endl;
   else
      cout << "The character 'L' in the locale is "
           << " not a control character." << endl;

   if ( result2 )
      cout << "The character-set 'backslash-n' in the locale\n is "
           << "a control character." << endl;
   else
      cout << "The character-set 'backslash-n' in the locale\n is "
           << " not a control character." << endl;

   if ( result3 )
      cout << "The character-set 'backslash-t' in the locale\n is "
           << "a control character." << endl;
   else
      cout << "The character-set 'backslash-n' in the locale \n is "
           << " not a control character." << endl;
}

输出结果:

The character 'L' in the locale is  not a control character.
The character-set 'backslash-n' in the locale
 is a control character.
The character-set 'backslash-t' in the locale
 is a control character.
d. isdigit()
static int Isdigit(
   RECHARTYPE ch 
) throw( );

该函数检查所传的字符是否是十进制数字字符。

i.参数

_Ch --这是要检查的字符。

ii.返回值

如果 _Ch 是一个数字,则该函数返回非零值,否则返回 0。

e. isgraph()
template<Class CharType>
   bool isgraph(
      CharType _Ch, 
      const locale& _Loc
   )

该函数用于检查所传的字符是否有图形表示法。

带有图形表示法的字符是除了空白字符(比如 ’ ')以外的所有可打印的字符。

i.参数

_Ch --这是要检查的字符。

_Loc --包含字母数字元素的区域设置进行测试。

ii.返回值

如果 _Ch 有图形表示法,则该函数返回非零值,否则返回 0。

f. islower()
template<Class CharType>
   bool islower(
      CharType _Ch, 
      const locale& _Loc
   )

该函数检查所传的字符是否是小写字母。

i.参数

_Ch --这是要检查的字符。

_Loc --包含字母数字元素的区域设置进行测试。

ii.返回值

如果 _Ch 是一个小写字母,则该函数返回非零值(true),否则返回 0(false)。

(3)其他函数

i. isprint()

该函数检查所传的字符是否是可打印的。可打印字符是非控制字符的字符。

ii. ispunct()

该函数检查所传的字符是否是标点符号字符。

iii. isspace()

该函数检查所传的字符是否是空白字符。

iv. isupper()

该函数检查所传的字符是否是大写字母。

v. isxdigit()

该函数检查所传的字符是否是十六进制数字。

3.<errno.h>

(1) 简介

C 标准库的 errno.h 头文件定义了整数变量 errno,它是通过系统调用设置的,在错误事件中的某些库函数表明了什么发生了错误。该宏扩展为类型为 int 的可更改的左值,因此它可以被一个程序读取和修改。

在程序启动时,errno 设置为零,C 标准库中的特定函数修改它的值为一些非零值以表示某些类型的错误。您也可以在适当的时候修改它的值或重置为零。

errno.h 头文件定义了一系列表示不同错误代码的宏,这些宏应扩展为类型为 int 的整数常量表达式。

(2) 库宏

a.
extern int errno; 

这是通过系统调用设置的宏,在错误事件中的某些库函数表明了什么发生了错误。

b.
 EDOM Domain Error

这个宏表示一个域错误,它在输入参数超出数学函数定义的域时发生,errno 被设置为 EDOM。

c.
ERANGE Range Error

(3) 错误码以及类型对应

可以修改上述代码里 errno 的值从而得出不同的错误类型,从 43 之后就一直是 Unknown error 错误类型。

Value of errno: 0
Error opening file: No error

Value of errno: 1
Error opening file: Operation not permitted

Value of errno: 2
Error opening file: No such file or directory

Value of errno: 3
Error opening file: No such process

Value of errno: 4
Error opening file: Interrupted function call

Value of errno: 5
Error opening file: Input/output error

Value of errno: 6
Error opening file: No such device or address

Value of errno: 7
Error opening file: Arg list too long

Value of errno: 8
Error opening file: Exec format error

Value of errno: 9
Error opening file: Bad file descriptor

Value of errno: 10
Error opening file: No child processes

Value of errno: 11
Error opening file: Resource temporarily unavailable

Value of errno: 12
Error opening file: Not enough space

Value of errno: 13
Error opening file: Permission denied

Value of errno: 14
Error opening file: Bad address

Value of errno: 15
Error opening file: Unknown error

Value of errno: 16
Error opening file: Resource device

Value of errno: 17
Error opening file: File exists

Value of errno: 18
Error opening file: Improper link

Value of errno: 19
Error opening file: No such device

Value of errno: 20
Error opening file: Not a directory

Value of errno: 21
Error opening file: Is a directory

Value of errno: 22
Error opening file: Invalid argument

Value of errno: 23
Error opening file: Too many open files in system

Value of errno: 24
Error opening file: Too many open files

Value of errno: 25
Error opening file: Inappropriate I/O control operation

Value of errno: 26
Error opening file: Unknown error

Value of errno: 27
Error opening file: File too large

Value of errno: 28
Error opening file: No space left on device

Value of errno: 29
Error opening file: Invalid seek

Value of errno: 30
Error opening file: Read-only file system

Value of errno: 31
Error opening file: Too many links

Value of errno: 32
Error opening file: Broken pipe

Value of errno: 33
Error opening file: Domain error

Value of errno: 34
Error opening file: Result too large

Value of errno: 35
Error opening file: Unknown error

Value of errno: 36
Error opening file: Resource deadlock avoided

Value of errno: 37
Error opening file: Unknown error

Value of errno: 38
Error opening file: Filename too long

Value of errno: 39
Error opening file: No locks available

Value of errno: 40
Error opening file: Function not implemented

Value of errno: 41
Error opening file: Directory not empty

Value of errno: 42
Error opening file: Illegal byte sequence

Value of errno: 43
Error opening file: Unknown error

Value of errno: 44
Error opening file: Unknown error

Value of errno: 45
Error opening file: Unknown error

Value of errno: 46
Error opening file: Unknown error

标签:opening,locale,errno,Value,C语言,标准,file,Error
来源: https://blog.csdn.net/weixin_52275937/article/details/116028976

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有