常用函数整理(基础篇)

目录

strcpy

返回值

strcat

Parameters

返回值

strncpy

Parameters

返回值

strncat

Parameters

返回值

strncmp

Parameters

返回值

strstr

Parameters

返回值

strtok

Parameters

返回值

getchar

返回值

putchar

Parameters

返回值

strcmp

Parameters

返回值

memset

Parameters

返回值

system

Parameters

参数

返回值

pow

Parameters

返回值

sqrt

Parameters

返回值

strlen

Parameters

返回值

islower

Parameters

返回值

isupper

Parameters

返回值

toupper

Parameters

返回值

tolower

Parameters

返回值

isalpha

Parameters

返回值

assert

Parameters

返回值

signal

Parameters

返回值

qsort

Parameters

返回值

strerror

Parameters

返回值

perror

Parameters.

返回值

isspace

Parameters

返回值

isdigit

Parameters

返回值

iscntrl

Parameters

返回值

isxdigit

Parameters

返回值

salnum

Parameters

返回值

ispunct

Parameters

返回值

isgraph

Parameters

返回值

isprint

Parameters

返回值

memcpy

Parameters

返回值

memmove

Parameters

返回值

memcmp

Parameters

返回值

offsetof

Parameters

返回值

malloc

Parameters

返回值

calloc

Parameters

返回值

realloc

Parameters

返回值

free

Parameters

返回值

fopen

Parameters

返回值

fclose

Parameters

返回值

fgetc

Parameters

返回值

fputc

Parameters

返回值

fgets

Parameters

返回值

fputs

Parameters

返回值

fscanf

Parameters

返回值

fprintf

Parameters

返回值

fread

Parameters

返回值

fwrite

Parameters

返回值

sprintf

Parameters

返回值

sscanf

Parameters

返回值

fseek

返回值

ftell

Parameters

返回值

rewind

Parameters

返回值

feof

Parameters

返回值

strcpy<string.h>

//字符串拷贝

char * strcpy ( char * destination, const char * source );

destination(目的地):

Pointer to the destination array where the content is to be copied.

指向要在其中复制内容的目标数组的指针。

source(源):

C string to be copied.

返回值

返回目标

/* strcpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

输出:


str1: Sample string
str2: Sample string
str3: copy successful

strcat<string.h>

//字符串追加

char * strcat ( char * destination, const char * source );

Parameters

destination(目的地):

Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.(指向目标数组的指针,该数组应包含 C 字符串,并且足够大以包含串联的结果字符串。)

source(源):

C string to be appended. This should not overlap destination.要追加的 C 字符串。这不应与目标重叠。()

返回值

返回目标

/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}

输出:


these strings are concatenated. 

strncpy<string.h>

//字符串拷贝,可选择拷贝多少

char * strncpy ( char * destination, const char * source, size_t num );

Parameters

destination(目的地)

Pointer to the destination array where the content is to be copied.(指向要在其中复制内容的目标数组的指针。)

source(源)

C string to be copied.(要复制的 C 字符串。)

num

Maximum number of characters to be copied from source.
size_t is an unsigned integral type.

数字

要从复制的最大字符数。
size_t 是无符号整数类型。

返回值

返回目标

/* strncpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]= "To be or not to be";
  char str2[40];
  char str3[40];

  /* copy to sized buffer (overflow safe): */
  strncpy ( str2, str1, sizeof(str2) );


  /* partial copy (only 5 chars): */
  strncpy ( str3, str2, 5 );
  str3[5] = '\0';   /* null character manually added */

  puts (str1);
  puts (str2);
  puts (str3);

  return 0;
}

输出:


To be or not to be
To be or not to be
To be 

strncat<string.h>

//字符串追加,可选择追加多少

char * strncat ( char * destination, const char * source, size_t num );

Parameters

destination

Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string, including the additional null-character.

source

C string to be appended.

num

Maximum number of characters to be appended.
size_t is an unsigned integral type.

目的地

指向目标数组的指针,该数组应包含一个 C 字符串,并且足够大以包含串联的结果字符串,包括其他 null 字符。

要追加的 C 字符串。

数字

要追加的最大字符数。
size_t是无符号整数类

型。

返回值

返回目标

/* strncat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[20];
  char str2[20];
  strcpy (str1,"To be ");
  strcpy (str2,"or not to be");
  strncat (str1, str2, 6);
  puts (str1);
  return 0;
}

输出:


To be or not

strncmp<string.h>

//字符串比较:

int strncmp ( const char * str1, const char * str2, size_t num );

Parameters

str1

C string to be compared.

str2

C string to be compared.

num

Maximum number of characters to compare.
size_t is an unsigned integral type.

str1

要比较的 C 字符串。

str2

要比较的 C 字符串。

数字

要比较的最大字符数。
size_t是无符号整数类型。

 

返回值

返回一个整数值,该值指示字符串之间的关系:

返回值 表明
<0 不匹配的第一个字符在 str1 中的值低于 str2 中的值
0 两个字符串的内容相等
>0 第一个不匹配的字符在 str1 中的值大于在 str2 中的值

/* strncmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
  int n;
  puts ("Looking for R2 astromech droids...");
  for (n=0 ; n<3 ; n++)
    if (strncmp (str[n],"R2xx",2) == 0)
    {
      printf ("found %s\n",str[n]);
    }
  return 0;
}

输出:


Looking for R2 astromech droids...
found R2D2
found R2A6

strstr<string.h>

//判断str2是不是str1的子串

const char * strstr ( const char * str1, const char * str2 );
      char * strstr (       char * str1, const char * str2 );

Parameters

str1

C string to be scanned.

str2

C string containing the sequence of characters to match.

str1

要扫描的 C 字符串。

str2

包含要匹配的字符序列的 C 字符串。

返回值

指向 str1 中指定的整个字符序列在 str2 中首次出现的指针,如果序列在 str1 中不存在,则为 null 指针。

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  if (pch != NULL)
    strncpy (pch,"sample",6);
  puts (str);
  return 0;
}

输出:


This is a sample string

strtok<string.h>

//分割字符串

char * strtok ( char * str, const char * delimiters );

Parameters

str

C string to truncate.
Notice that this string is modified by being broken into smaller strings (tokens).
Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.

delimiters

C string containing the delimiter characters.
These can be different from one call to another.

str

要截断的 C 字符串。
请注意,此字符串是通过分解为较小的字符串(标记)来修改的。
或者,可以指定空指针,在这种情况下,函数将继续扫描以前成功调用函数的位置。

分隔符

包含分隔符字符的 C 字符串。
这些可能因调用而异。

返回值

如果找到令牌,则指向令牌开头的指针。
否则为空指针
当在正在扫描的字符串中到达字符串的末尾(即空字符)时,始终返回空指针

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}

输出:


Splitting string "- This, a sample string." into tokens:
This
a
sample
string

getchar<stdio.h>

//输入

int getchar ( void );

返回值

成功后,将返回字符读取(提升为 int 值)。
返回类型为 int 以适应特殊值 EOF,该值指示失败:
如果标准输入位于文件末尾,则该函数返回 EOF 并设置 stdin 的 eof 指示器 (feof)。
如果发生其他读取错误,该函数还会返回 EOF,但改为设置其错误指示器 (ferror)。

putchar<stdio.h>

//输出

int putchar ( int character );

Parameters

character

The int promotion of the character to be written.
The value is internally converted to an unsigned char when written.

字符

要编写的字符的 int 提升。
写入时,该值在内部转换为无符号字符。

返回值

成功后,将返回所写字符
如果发生写入错误,则返回 EOF 并设置错误指示器ferror)。

strcmp<string.h>

//字符串比较

int strcmp ( const char * str1, const char * str2 );

Parameters

str1

C string to be compared.

str2

C string to be compared.

str1

要比较的 C 字符串。

str2

要比较的 C 字符串。

返回值

返回一个整数值,该值指示字符串之间的关系:

返回值

表明
<0 第一个不匹配的字符在 PTR1 中的值低于 PTR2 中的值
0 两个字符串的内容相等
>0 第一个不匹配的字符在 PTR1 中的值大于在 PTR2 中的值

memset<string.h>

//内存设置将这个地址开始的num个数设置为value

void * memset ( void * ptr, int value, size_t num );

Parameters

ptr

Pointer to the block of memory to fill.

value

Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.

num

Number of bytes to be set to the value.
size_t is an unsigned integral type.

PTR

指向要填充的内存块的指针。

价值

要设置的值。该值作为 int 传递,但该函数使用此无符号 char 转换填充内存块。

数字

要设置为该值的字节数。
size_t 是无符号整数类型。

返回值

返回 PTR

/* memset example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "almost every programmer should know memset!";
  memset (str,'-',6);
  puts (str);
  return 0;
}

输出:

------ every programmer should know memset!

system<stdlib.h>

//系统命令操作

int system (const char* command);

Parameters

command

C-string containing the system command to be executed.
Or, alternatively, a null pointer, to check for a command processor.

参数

命令

包含要执行的系统命令的 C 字符串。
或者,或者使用空指针来检查命令处理器。

返回值

如果为 null 指针,则该函数在命令处理器可用时返回非零值,如果不可用,则返回零值。

如果不是空指针,则返回的值取决于系统和库实现,但通常应是被调用命令返回的状态代码, 如果支持。

pow<math.h>

//平方

   double pow  (double base     , double exponent);
      float powf (float base      , float exponent);
long double powl (long double base, long double exponent);

Parameters

base

Base value.

exponent

Exponent value.

基础

基值。

指数

指数值。

返回值

提高权力的结果.

如果 是有限负数,而 是有限的但不是整数值,则会导致域错误
如果两者均为零,则还可能导致某些实现出现域错误
如果为零且为负数,则可能会导致错误或极点错误(或无,具体取决于库实现)。
如果结果太大或太小而无法用返回类型的值表示,则该函数也可能导致范围错误

sqrt<math.h>

//开平方

  • C99
     double sqrt  (double x);
      float sqrtf (float x);
long double sqrtl (long double x);

Parameters

x

Value whose square root is computed.
If the argument is negative, a domain error occurs.

x

计算平方根的值。
如果参数为负数,则会发生域错误

返回值

的平方根。
如果为负数,则发生域错误:xx

/* sqrt example */
#include <stdio.h>      /* printf */
#include <math.h>       /* sqrt */

int main ()
{
  double param, result;
  param = 1024.0;
  result = sqrt (param);
  printf ("sqrt(%f) = %f\n", param, result );
  return 0;
}

输出:


sqrt(1024.000000) = 32.000000

strlen<string.h.h>

//计算字符串长度,‘\0’结束

size_t strlen ( const char * str );

Parameters

str

C string.

str

C 字符串。

返回值

字符串的长度。

/* strlen example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char szInput[256];
  printf ("Enter a sentence: ");
  gets (szInput);
  printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
  return 0;
}

输出:

Enter sentence: just testing
The sentence entered is 12 characters long.

islower<ctype.h>

//判断小写字母

int islower ( int c );

Parameters

c

Character to be checked, casted to an int, or EOF.

c

要检查、转换为 int 或 EOF 的字符。

返回值

如果 c 确实是小写字母,则与零(即 true)不同的值。否则为零(即假)。

isupper<ctype.h>

//判断大写字母

int isupper ( int c );

Parameters

c

Character to be checked, casted to an int, or EOF.

c

要检查、转换为 int 或 EOF 的字符。

返回值

一个不同于零的值(即 true),如果 c 确实是一个大写字母。否则为零(即假)。

toupper<ctype.h>

//小写——>大写

int toupper ( int c );

Parameters

c

Character to be converted, casted to an int, or EOF.

c

要转换、转换为 int 或 EOF 的字符。

返回值

大写等效于 c(如果存在此类值),否则为 c(未更改)。该值作为 int 值返回,该值可以隐式转换为 char。

tolower<ctype.h>

//大写——>小写

int tolower ( int c );

Parameters

c

Character to be converted, casted to an int, or EOF.

c

要转换、转换为 int 或 EOF 的字符。

返回值

小写等效于 c(如果存在此类值),否则为 c(未更改)。
该值作为 int 值返回,该值可以隐式转换为 char。

isalpha<ctype.h>

//判断是不是字母

int isalpha ( int c );

Parameters

c

Character to be checked, casted to an int, or EOF.

c

要检查、转换为 int 或 EOF 的字符。

返回值

一个不同于零的值(即 true),如果 c 确实是一个字母。否则为零(即假)。

assert<assert.h>

//断言

void assert (int expression);

Parameters

expression

Expression to be evaluated. If this expression evaluates to 0, this causes an assertion failure that terminates the program.

表达

要计算的表达式。如果此表达式的计算结果为 NULL,则会导致断言失败,从而终止程序.

返回值

没有

signal<signal.h>

//信号处理

void (*signal(int sig, void (*func)(int)))(int);

Parameters

sig

处理功能设置为的信号值。以下宏常量表达式标识标准信号值:

宏观 信号
SIGABRT (信号中止)异常终止,例如由函数启动。abort
SIGFPE (信号浮点异常)错误的算术运算,例如零除法或导致溢出的运算(不一定使用浮点运算)。
SIGILL (信号非法指令)无效的函数图像,例如非法指令。这通常是由于代码损坏或尝试执行数据。
SIGINT (信号中断)交互式注意力信号。通常由应用程序用户生成。
SIGSEGV (信号分割违规)对存储的无效访问:当程序尝试读取或写入其分配的内存时。
SIGTERM (信号终止)终止请求发送到程序。


每个库实现都可以提供可与此函数一起使用的其他信号值宏常量。

请注意,并非所有运行环境都需要生成自动信号,即使在上述特定情况下也是如此,尽管所有运行环境都必须传递通过显式调用函数生成的信号。raise

函数

指向函数的指针。这可以是由程序员定义的函数,也可以是以下预定义函数之一:

SIG_DFL 默认处理:信号由该特定信号的默认操作处理。
SIG_IGN 忽略信号:忽略信号。

返回值

返回类型与参数的类型相同。

如果请求成功,该函数将返回指向特定处理程序函数的指针,该处理程序函数负责在调用之前处理此信号(如果有)。或者,或者在调用之前,信号由默认处理程序处理或被忽略。

如果函数未成功注册新的信号处理程序,它将返回并可能设置为正值。

qsort<stdlib.h>

//排序任意类型数据

void qsort (void* base, size_t num, size_t size,
            int (*compar)(const void*,const void*));

Parameters

base

Pointer to the first object of the array to be sorted, converted to a void*.

num

Number of elements in the array pointed to by base.
size_t is an unsigned integral type.

size

Size in bytes of each element in the array.
size_t is an unsigned integral type.

compar

Pointer to a function that compares two elements.
This function is called repeatedly by qsort to compare two elements. It shall follow the following prototype:

基础

指向要排序的数组的第一个对象的指针,转换为 .void*

数字

数组中由 指向的元素数。
是无符号整型。basesize_t

大小

数组中每个元素的大小(以字节为单位)。
是无符号整型。size_t

比较

指向比较两个元素的函数的指针。
重复调用此函数以比较两个元素。它应遵循以下原型:qsort

将两个指针作为参数(都转换为常量 void*)。该函数通过返回(以稳定和传递的方式)来定义元素的顺序:

返回值 意义
<0 指向的元素 在指向的元素之前p1p2
0 指向的元素等效于p1p2
>0 指向的元素在指向的元素之后p1p2

返回值

没有

strerror<string.h>

//获取错误消息字符串的指针

char * strerror ( int errnum );

Parameters

errnum

Error number.

错误号。

返回值

指向描述错误错误的字符串的指针。

/* strerror example : error list */
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main ()
{
  FILE * pFile;
  pFile = fopen ("unexist.ent","r");
  if (pFile == NULL)
    printf ("Error opening file unexist.ent: %s\n",strerror(errno));
  return 0;
}

可能的输出:


Error opening file unexist.ent: No such file or directory

perror<stdio.h>

//根据errno中保存的错误代码,打印出来

void perror ( const char * str );

Parameters.

str

C string containing a custom message to be printed before the error message itself.
If it is a null pointer, no preceding custom message is printed, but the error message is still printed.
By convention, the name of the application itself is generally used as parameter.

str

C 字符串,包含要在错误消息本身之前打印的自定义消息。
如果是空指针,则不会打印前面的自定义消息,但仍会打印错误消息。
按照惯例,应用程序本身的名称通常用作参数。

返回值

没有

/* perror example */
#include <stdio.h>

int main ()
{
  FILE * pFile;
  pFile=fopen ("unexist.ent","rb");
  if (pFile==NULL)
    perror ("The following error occurred");
  else
    fclose (pFile);
  return 0;
}


如果文件 unexist.ent 不存在,则程序输出可能需要与此类似的内容:

The following error occurred: No such file or directory

isspace<ctype.h>

//判断是不是空白字符

int isspace ( int c );

Parameters

c

Character to be checked, casted to an int, or EOF.

c

要检查、转换为 int 或 EOF 的字符。

返回值

一个不同于零的值(即 true),如果 c 确实是一个空格字符。否则为零(即假)。

/* isspace example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  char c;
  int i=0;
  char str[]="Example sentence to test isspace\n";
  while (str[i])
  {
    c=str[i];
    if (isspace(c)) c='\n';
    putchar (c);
    i++;
  }
  return 0;
}

此代码逐个字符打印出 C 字符串,将任何空格字符替换为换行符。输出:

Example
sentence
to
test
isspace

isdigit<ctype.h>

//判断是不是十进制0——9的数

int isdigit ( int c );

Parameters

c

Character to be checked, casted to an int, or EOF.

c

要检查、转换为 int 或 EOF 的字符。

返回值

如果 c 确实是十进制数字,则与零(即 true)不同的值。否则为零(即假)。

/* isdigit example */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
  char str[]="1776ad";
  int year;
  if (isdigit(str[0]))
  {
    year = atoi (str);
    printf ("The year that followed %d was %d.\n",year,year+1);
  }
  return 0;
}

输出


The year that followed 1776 was 1777

iscntrl<ctype.h>

//判断控制字符。如:“\t”或“%d”这种

int iscntrl ( int c );

Parameters

c

Character to be checked, casted to an int, or EOF.

c

要检查、转换为 int 或 EOF 的字符。

返回值

如果 c 确实是控制字符,则与零(即 true)不同的值。否则为零(即假)。

isxdigit<ctype.h>

//判断是不是十六进制数字

int isxdigit ( int c );

Parameters

c

Character to be checked, casted to an int, or EOF.

c

要检查、转换为 int 或 EOF 的字符。

返回值

一个不同于零的值(即 true),如果 c 确实是十六进制数字。否则为零(即假)。

salnum<ctype.h>

//判断是不是字母/数字

int isalnum ( int c );

Parameters

c

Character to be checked, casted as an int, or EOF.

c

要检查的字符,转换为 int 或 EOF。

返回值

一个不同于零的值(即 true),如果 c 确实是数字或字母。否则为零(即假)。

ispunct<ctype.h>

//判断是不是标点字符(任何不属于字母/数字的图形字符)

int ispunct ( int c );

Parameters

c

Character to be checked, casted to an int, or EOF.

c

要检查、转换为 int 或 EOF 的字符。

返回值

如果 c 确实是标点符号,则与零(即 true)不同的值。否则为零(即假)。

isgraph<ctype.h>

//判断是不是任何图形字符

int isgraph ( int c );

Parameters

c

Character to be checked, casted to an int, or EOF.

c

要检查、转换为 int 或 EOF 的字符。

返回值

一个不同于零的值(即 true),如果 c 确实具有图形表示为字符。否则为零(即假)。

isprint <ctype.h>

//判断是不是任何可打印字符

int isprint ( int c );

Parameters

c

Character to be checked, casted to an int, or EOF.

c

要检查、转换为 int 或 EOF 的字符。

返回值

如果 c 确实是可打印字符,则与零(即 true)不同的值。否则为零(即假)。

memcpy<string.h>

//内存拷贝(不能拷贝自己)

void * memcpy ( void * destination, const void * source, size_t num );

Parameters

destination

Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.

source

Pointer to the source of data to be copied, type-casted to a pointer of type const void*.

num

Number of bytes to copy.
size_t is an unsigned integral type.

目的地

指向要在其中复制内容的目标数组的指针,类型转换为 void* 类型的指针。

指向要复制的数据源的指针,类型转换为 const void* 类型的指针。

数字

要复制的字节数。
size_t 是无符号整数类型。

返回值

返回目标

memmove<string.h>

//内存拷贝(可拷贝自己)

void * memmove ( void * destination, const void * source, size_t num );

Parameters

destination

Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.

source

Pointer to the source of data to be copied, type-casted to a pointer of type const void*.

num

Number of bytes to copy.
size_t is an unsigned integral type.

目的地

指向要在其中复制内容的目标数组的指针,类型转换为 void* 类型的指针。

指向要复制的数据源的指针,类型转换为 const void* 类型的指针。

数字

要复制的字节数。
size_t 是无符号整数类型。

返回值

返回目标

memcmp<string.h>

//内存比较

int memcmp ( const void * ptr1, const void * ptr2, size_t num );

Parameters

ptr1

Pointer to block of memory.

ptr2

Pointer to block of memory.

num

Number of bytes to compare.

PTR1

指向内存块的指针。

PTR2

指向内存块的指针。

数字

要比较的字节数。

返回值

返回一个整数值,该值指示内存块内容之间的关系:
返回值 表明
<0 两个内存块中不匹配的第一个字节在 PTR1 中的值低于 PTR2 中的值(如果评估为无符号字符值)
0 两个内存块的内容相等
>0 两个内存块中不匹配的第一个字节在 PTR1 中的值大于在 PTR2 中的值(如果评估为无符号字符值)

offsetof<stddef.h>

//计算偏移量(结构体在内存)

offsetof (type,member)

Parameters

type

A type in which member is a valid member designator.

  • C++98

type shall be a structure or union type.

member

A member of type.

类型

成员为有效成员指示符的类型。

成员

类型的成员。

返回值

类型为 size_t 的值,其偏移值为类型的成员

malloc<stdlib.h>

//动态内存的开辟

void* malloc (size_t size);

Parameters

size

Size of the memory block, in bytes.
size_t is an unsigned integral type.

大小

内存块的大小(以字节为单位)。
是无符号整型。size_t

返回值

成功时,指向函数分配的内存块的指针。
此指针的类型始终为 ,可以强制转换为所需类型的数据指针,以便可取消引用。
如果函数无法分配请求的内存块,则返回空指针void*

calloc<stdlib.h>

//动态内存的开辟并初始化为0

void* calloc (size_t num, size_t size);

Parameters

num

Number of elements to allocate.

size

Size of each element.

数字

要分配的元素数。

大小

每个元素的大小。
size_t是无符号整型。

返回值

成功时,指向函数分配的内存块的指针。
此指针的类型始终为 ,可以强制转换为所需类型的数据指针,以便可取消引用。
如果函数无法分配请求的内存块,则返回空指针void*

realloc<stdlib.h>

//动态内存扩容

void* realloc (void* ptr, size_t size);

Parameters

ptr

Pointer to a memory block previously allocated with malloccalloc or realloc.
Alternatively, this can be a null pointer, in which case a new block is allocated (as if malloc was called).

size

New size for the memory block, in bytes.
size_t is an unsigned integral type.

PTR

指向先前分配有 、 或 的内存块的指针。
或者,这可以是一个空指针,在这种情况下,将分配一个新块(就像被调用一样)。malloccallocreallocmalloc

大小

内存块的新大小(以字节为单位)。
是无符号整型。size_t

返回值

指向重新分配的内存块的指针,该内存块可能与新位置相同,也可能是新位置。
此指针的类型为 ,可以强制转换为所需类型的数据指针,以便可取消引用。ptrvoid*

free<stdlib.h>

//释放动态开辟的内存

void free (void* ptr);

Parameters

ptr

Pointer to a memory block previously allocated with malloccalloc or realloc.

PTR

指向先前分配有 、 或 的内存块的指针。malloccallocrealloc

返回值

没有

fopen<stdio.h>

//打开文件

FILE * fopen ( const char * filename, const char * mode );

Parameters

filename

C string containing the name of the file to be opened.
Its value shall follow the file name specifications of the running environment and can include a path (if supported by the system).

mode

C string containing a file access mode. 

文件名

包含要打开的文件的名称的 C 字符串。
其值应遵循运行环境的文件名规范,并且可以包含路径(如果系统支持)。

模式

包含文件访问模式的 C 字符串。

返回值

如果文件成功打开,该函数将返回指向 FILE 对象的指针,该对象可用于在将来的操作中标识流。
否则,将返回空指针。
在大多数库实现中,errno 变量在失败时也设置为特定于系统的错误代码。

fclose<stdio.h>

//关闭文件

int fclose ( FILE * stream );

Parameters

stream

Pointer to a FILE object that specifies the stream to be closed.

指向指定要关闭的流的 FILE 对象的指针。

返回值

如果流成功关闭,则返回零值。
失败时,将返回 EOF

fgetc<stdio.h>

//字符输入

int fgetc ( FILE * stream );

Parameters

stream

Pointer to a FILE object that identifies an input stream.

指向标识输入流的 FILE 对象的指针。

返回值

成功后,将返回字符读取(提升为 int 值)。
返回类型为 int 以适应特殊值 EOF,该值表示失败:
如果位置指示器位于文件末尾,则该函数返回 EOF 并设置的 eof 指示器 (feof)。
如果发生其他读取错误,该函数还会返回 EOF,但改为设置其错误指示器 (ferror)。

fputc<stdio.h>

//字符输出

int fputc ( int character, FILE * stream );

Parameters

character

The int promotion of the character to be written.
The value is internally converted to an unsigned char when written.

stream

Pointer to a FILE object that identifies an output stream.

字符

要编写的字符的 int 提升。
写入时,该值在内部转换为无符号字符。

指向标识输出流的 FILE 对象的指针。

返回值

成功后,将返回所写字符
如果发生写入错误,则返回 EOF 并设置错误指示器ferror)。

fgets<stdio.h>

//字符按行输入

char * fgets ( char * str, int num, FILE * stream );

Parameters

str

Pointer to an array of chars where the string read is copied.

num

Maximum number of characters to be copied into str (including the terminating null-character).

stream

Pointer to a FILE object that identifies an input stream.
stdin can be used as argument to read from the standard input.

str

指向在其中复制字符串读取的字符数组的指针。

数字

要复制到 str 的最大字符数(包括终止空字符)。

指向标识输入流的 FILE 对象的指针。
stdin 可以用作从标准输入读取的参数。

返回值

成功后,函数返回 str
如果在尝试读取字符时遇到文件末尾,则设置 eof 指示器 (feof)。如果在读取任何字符之前发生这种情况,则返回的指针为空指针(str 的内容保持不变)。
如果发生读取错误,则设置错误指示器ferror),并返回空指针(但str指向的内容可能已更改)。

fputs<stdio.h>

//字符按行输出

int fputs ( const char * str, FILE * stream );

Parameters

str

C string with the content to be written to stream.

stream

Pointer to a FILE object that identifies an output stream.

str

包含要写入的内容的 C 字符串

指向标识输出流的 FILE 对象的指针。

返回值

成功时,将返回非负值。
出错时,该函数返回 EOF 并设置错误指示器ferror)。

fscanf<stdio.h>

//格式化(scanf(这里面的就是格式化))输入

int fscanf ( FILE * stream, const char * format, ... );

Parameters

stream

Pointer to a FILE object that identifies the input stream to read data from.

format

C string that contains a sequence of characters that control how characters extracted from the stream are treated:

指向标识要从中读取数据的输入流的 FILE 对象的指针。

格式

C 字符串,包含一系列字符,这些字符控制如何处理从流中提取的字符:

  • 空格字符:该函数将读取并忽略在下一个非空格字符之前遇到的任何空格字符(空格字符包括空格、换行符和制表符 - 请参阅 isspace)。格式字符串中的单个空格验证从中提取的任意数量的空格字符(包括无)。
  • 非空格字符,格式说明符 (%) 除外:任何不是空格字符(空白、换行符或制表符)或格式说明符的一部分(以 % 字符开头)的字符都会导致函数从流中读取下一个字符,将其与此非空格字符进行比较,如果匹配,则将其丢弃,函数继续使用格式的下一个字符。如果字符不匹配,函数将失败,返回流的后续字符并使其未读状态。
  • 格式说明符:由初始百分号 (%) 形成的序列表示格式说明符,用于指定要从中检索并存储到附加参数所指向的位置的数据的类型和格式。


fscanf 的格式说明符遵循以下原型:

%[*][宽度][长度]说明符 其中末尾的说明

字符是最重要的组成部分,因为它定义了提取哪些字符、它们的解释及其相应参数的类型:

规范 描述 提取的字符
i,u 整数 任意数量的数字,可选在前面加上符号(+或-)。
默认采用十进制数字 (0-9),但 0 前缀引入八进制数字 (0-7) 和 0x 十六进制数字 (0-f)。
d 十进制整数 任意数量的十进制数字 (0-9),可选前面加一个符号(+或 -)。
o 八进制整数 任意数量的八进制数字 (0-7),可选地在前面加上符号 (+或 -)。
x 十六进制整数 任意数量的十六进制数字(0-9、a-f、A-F),可以选择在 0x 或 0X 前面加上,并且所有数字前面都可以选择带有符号(+或 -)。
f, e, g 浮点数 一系列十进制数字,可以选择包含小数点,可以选择在前面加上符号(+或-),可以选择后跟e或E字符以及十进制整数(或支持的其他一些序列)。
符合 C99 的实现还支持前面带有 或 的十六进制浮点格式。strtod0x0X
一个
c 字符 下一个字符。如果指定了 1 以外的宽度,则该函数将读取精确宽度字符,并将它们存储在作为参数传递的数组的连续位置。末尾不附加空字符。
s 字符串 任意数量的非空格字符,在找到的第一个空格字符处停止。终止空字符会自动添加到存储序列的末尾。
p 指针地址 表示指针的字符序列。使用的特定格式取决于系统和库实现,但它与用于在 fprintf 中格式化 %p 的格式相同。
[字符] 扫描集 方括号之间指定的任意数量的字符。
不是第一个字符的短划线 (-) 可能会在某些库实现中产生不可移植的行为。
[^字符] 否定扫描集 任意数量的字符,其中没有一个指定为括号之间的字符
n 计数 不消耗任何输入。
到目前为止读取的字符数存储在指向的位置。stream
% % 一个 % 后跟另一个 % 与单个 % 匹配。

除 n 外,任何说明符至少应使用一个字符。否则,匹配将失败,扫描到此结束。

格式说明符还可以包含子说明符星号 (*)、宽度长度(按此顺序),它们是可选的,遵循以下规范:

子说明符 描述
* 可选的起始星号表示数据将从流中读取但被忽略(即它不存储在参数指向的位置)。
width 指定当前读取操作中要读取的最大字符数(可选)。
length hh, h, l, ll, j, z, t, L(可选)之一。
这会更改相应参数所指向的预期存储类型(见下文)。


这是一个图表,显示了存储输入的相应参数的预期类型(带和不带长度子说明符):

说明符
长度 D i U o x F e g a c s [] [^] p n
(none) 国际* 无符号 int* 浮* 煳* 无效** 国际*
呵呵 签名字符* 无符号字符* 签名字符*
h 短内线* 无符号短整型* 短内线*
l 多头整型* 无符号长整型* 双* wchar_t* 多头整型*
ll 长 长 长 内特* 无符号长长 int* 长 长 长 内特*
j intmax_t* uintmax_t* intmax_t*
z size_t* size_t* size_t*
t ptrdiff_t* ptrdiff_t* ptrdiff_t*
L 长双*

注意:黄色行表示 C99 引入的说明符和子说明符。

返回值

成功后,该函数返回成功填充的参数列表的项数。此计数可以与预期的项目数匹配,也可以由于匹配失败、读取错误或文件末尾的到达而减少(甚至为零)。

如果发生读取错误或在读取时到达文件末尾,则会设置正确的指示器(feof 或 ferror)。并且,如果在成功读取任何数据之前发生任一情况,则返回 EOF

如果在解释宽字符时发生编码错误,该函数会将 errno 设置为 EILSEQ。

fprintf<stdio.h>

//格式化(scanf(这里面的就是格式化))输出

int fprintf ( FILE * stream, const char * format, ... );

Parameters

stream

Pointer to a FILE object that identifies an output stream.

format

C string that contains the text to be written to the stream.

指向标识输出流的 FILE 对象的指针。

格式

包含要写入流的文本的 C 字符串。
它可以选择性地包含嵌入的格式说明符,这些说明符由后续附加参数中指定的值替换,并根据请求设置格式。

格式说明符遵循此原型: %[flags][width][.precision][length]说明符 其中末尾的说明

字符是最重要的组成部分,因为它定义了其相应参数的类型和解释:
 

规范 输出
 i 有符号十进制整数 392
u 无符号十进制整数 7235
o 无符号八进制 610
x 无符号十六进制整数 7FA
X 无符号十六进制整数(大写) 7FA
f 十进制浮点数,小写 392.65
F 十进制浮点数,大写 392.65
e 科学记数法(尾数/指数),小写 3.9265e+2
E 科学记数法(尾数/指数),大写 3.9265E+2
g 使用最短表示形式:%e 或 %f 392.65
G 使用最短表示形式:%E 或 %F 392.65
一个 十六进制浮点数,小写 -0xc.90FEP-2
一个 十六进制浮点数,大写 -0XC.90FEP-2
c 字符 一个
s 字符串 样本
p 指针地址 b8000000
n 没有打印任何内容。
相应的参数必须是指向有符号 int 的指针。
到目前为止写入的字符数存储在指向的位置。
% 一个 % 后跟另一个 % 字符会将单个 % 写入流。 %


格式说明符还可以包含子说明符标志宽度.precision 和修饰符(按此顺序),它们是可选的,并遵循以下规范:

标志 描述
- 在给定字段宽度内左对齐;右对齐是默认值(请参阅宽度子说明符)。
+ 即使在正数中,也会在结果前面加上加号或减号(+或-)。默认情况下,只有负数前面带有 - 符号。
(空格) 如果不打算写入任何符号,则会在值之前插入一个空格。
# 与 o、x 或 X 说明符一起使用时,对于小于零的值,值前面分别带有 0、0x 或 0X。
与 a、A、e、E、f、F、g 或 G 一起使用时,它强制写入输出包含小数点,即使后面没有更多的数字。 默认情况下,如果没有数字后跟,则不写入小数点。
0 指定填充时,用零 (0) 而不是空格左填充数字(请参阅宽度子说明符)。

宽度 描述
(数字) 要打印的最小字符数。如果要打印的值小于此数字,则用空格填充结果。即使结果更大,也不会截断该值。
* 宽度不是在格式字符串中指定的,而是作为必须格式化的参数之前的附加整数值参数指定的。

。精度 描述
. 对于整数说明符 (d, i, o, u, x, X):精度指定要写入的最小位数。如果要写入的值短于此数字,则结果将用前导零填充。即使结果较长,也不会截断该值。精度为 0 表示不为值 0 写入任何字符。
对于 a、a、e、e、f 和 F 说明符:这是小数点要打印的位数(默认情况下为 6)。
对于 g 和 G 说明符:这是要打印的最大有效位数。
对于 s:这是要打印的最大字符数。默认情况下,将打印所有字符,直到遇到结尾空字符。
如果在指定期间时没有显式精度值,则假定为 0。
.* 精度不是在格式字符串中指定的,而是作为必须格式化的参数之前的附加整数值参数指定的。


长度子说明符修改数据类型的长度。这是一个图表,显示用于解释带有和不带有长度说明符的相应参数的类型(如果使用不同的类型,则执行正确的类型提升或转换(如果允许):

说明符
长度 D i u o x X f F e E g G a A c s p n
(无) 国际 无符号整数 国际 煳* 无效* 国际*
呵呵 签名字符 无符号字符 签名字符*
h 短内 无符号短整型 短内线*
l 长整 无符号长整型 wint_t wchar_t* 多头整型*
长 长 无符号长长 长 长 长 内特*
j intmax_t uintmax_t intmax_t*
z size_t size_t size_t*
t ptrdiff_t ptrdiff_t ptrdiff_t*
L 长双

请注意,c 说明符将 int(或 wint_t)作为参数,但在格式化 char 值(或 wchar_t)以进行输出之前执行正确的转换。

注意:黄色行表示 C99 引入的说明符和子说明符。有关扩展类型的说明符,请参阅。<cinttypes>

返回值

成功后,将返回写入的字符总数。

如果发生写入错误,则设置错误指示器ferror)并返回负数。

如果在写入宽字符时发生多字节字符编码错误,errno 将设置为 EILSEQ 并返回负数。

fread<stdio.h>

//二进制方式读取

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

Parameters

ptr

Pointer to a block of memory with a size of at least (size*count) bytes, converted to a void*.

size

Size, in bytes, of each element to be read.
size_t is an unsigned integral type.

count

Number of elements, each one with a size of size bytes.
size_t is an unsigned integral type.

stream

Pointer to a FILE object that specifies an input stream.

PTR

指向大小至少为 (size*count) 字节的内存块的指针,转换为 void*。

大小

要读取的每个元素的大小(以字节为单位)。
size_t是无符号整数类型。

计数

元素数,每个元素的大小为字节大小
size_t 是无符号整数类型。

指向指定输入流的 FILE 对象的指针。

返回值

返回成功读取的元素总数。
如果此数字与 count 参数不同,则表示读取时发生读取错误或到达文件末尾。在这两种情况下,都会设置正确的指标,可以分别用 ferror 和 feof 进行检查。
如果大小计数为零,则该函数返回零,并且流状态和 ptr 指向的内容保持不变。
size_t 是无符号整数类型。

fwrite<stdio.h>

//二进制方式写到文件

size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

Parameters

ptr

Pointer to the array of elements to be written, converted to a const void*.

size

Size in bytes of each element to be written.
size_t is an unsigned integral type.

count

Number of elements, each one with a size of size bytes.
size_t is an unsigned integral type.

stream

Pointer to a FILE object that specifies an output stream.

PTR

指向要写入的元素数组的指针,转换为 const void*。

大小

要写入的每个元素的大小(以字节为单位)。
size_t是无符号整数类型。

计数

元素数,每个元素的大小为字节大小
size_t 是无符号整数类型。

指向指定输出流的 FILE 对象的指针。

返回值

返回成功写入的元素总数。
如果此数字与 count 参数不同,则写入错误阻止函数完成。在这种情况下,将为设置错误指示器ferror)。
如果大小计数为零,则该函数返回零,错误指示器保持不变。
size_t 是无符号整数类型。

sprintf<stdio.h>

//把格式化的数据写到字符串中

int sprintf ( char * str, const char * format, ... );

Parameters

str

Pointer to a buffer where the resulting C-string is stored.
The buffer should be large enough to contain the resulting string.

format

C string that contains a format string that follows the same specifications as format in printf (see printf for details).

str

指向存储生成的 C 字符串的缓冲区的指针。
缓冲区应足够大以包含生成的字符串。

格式

C 字符串,其中包含一个格式字符串,该字符串遵循与 printf 中的格式相同的规范(有关详细信息,请参阅 printf)。

返回值

成功后,将返回写入的字符总数。此计数不包括自动追加在字符串末尾的其他 null 字符。
失败时,返回负数。

sscanf<stdio.h>

//从字符串转化出格式化数据

int sscanf ( const char * s, const char * format, ...);

Parameters

s

C string that the function processes as its source to retrieve the data.

format

C string that contains a format string that follows the same specifications as format in scanf (see scanf for details).

s

函数处理作为其源以检索数据的 C 字符串。

格式

C 字符串,其中包含一个格式字符串,该字符串遵循与 scanf 中的格式相同的规范(有关详细信息,请参阅 scanf

返回值

成功后,该函数返回参数列表中成功填充的项数。此计数可以与预期的项目数匹配,或者在匹配失败的情况下更少(甚至为零)。
如果在成功解释任何数据之前输入失败,则返回 EOF

fseek<stdio.h>

//定位文件指针位置

int fseek ( FILE * stream, long int offset, int origin );

stream

Pointer to a FILE object that identifies the stream.

offset

Binary files: Number of bytes to offset from origin.
Text files: Either zero, or a value returned by ftell.

origin

Position used as reference for the offset. It is specified by one of the following constants defined in <cstdio> exclusively to be used as arguments for this function:

指向标识流的 FILE 对象的指针。

抵消

二进制文件:要从偏移的字节数。
文本文件:零或 ftell 返回的值。

起源

用作偏移参考的位置。它由 <cstdio> 中定义的以下常量之一指定,专门用作此函数的参数:

不断 参考位置
SEEK_SET 文件开头
SEEK_CUR 文件指针的当前位置
SEEK_END 文件结尾 *

返回值

如果成功,该函数将返回零。
否则,它将返回非零值。
如果发生读取或写入错误,则设置错误指示器ferror)。

ftell<stdio.h>

//返回文件指针相对于起始位置的偏移量

long int ftell ( FILE * stream );

Parameters

stream

Pointer to a FILE object that identifies the stream.

指向标识流的 FILE 对象的指针。

返回值

成功后,返回仓位指标的当前值。
失败时,返回 -1L,并将 errno 设置为特定于系统的正值。

rewind<stdio.h>

//让文件指针位置回到起始位置

void rewind ( FILE * stream );

Parameters

stream

Pointer to a FILE object that identifies the stream.

指向标识流的 FILE 对象的指针。

返回值

没有

feof<stdio.h>

//当文件读取结束时,判断结束原因是不是:遇到文件结束

int feof ( FILE * stream );

Parameters

stream

Pointer to a FILE object that identifies the stream.

指向标识流的 FILE 对象的指针。

返回值

如果设置了与流关联的文件结束指示符,则返回非零值。
否则,返回零。

猜你喜欢

转载自blog.csdn.net/weixin_71964780/article/details/132117027
今日推荐