C语言关于字符串的操作函数都有哪些?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言关于字符串的操作函数都有哪些?相关的知识,希望对你有一定的参考价值。

参考技术A 字符串连接函数:strcat
字符串复制函数:strcpy
字符串比较函数:strcmp
测字符串长度函数:strlen
参考技术B 字符串的截取,字符串的排序 参考技术C /*
* string.h
* This file has no copyright assigned and is placed in the Public Domain.
* This file is a part of the mingw-runtime package.
* No warranty is given; refer to the file DISCLAIMER within the package.
*
* Definitions for memory and string functions.
*
*/

#ifndef _STRING_H_
#define _STRING_H_

/* All the headers include this file. */
#include <_mingw.h>

/*
* Define size_t, wchar_t and NULL
*/
#define __need_size_t
#define __need_wchar_t
#define __need_NULL
#ifndef RC_INVOKED
#include <stddef.h>
#endif /* Not RC_INVOKED */

#ifndef RC_INVOKED

#ifdef __cplusplus
extern "C"
#endif

/*
* Prototypes of the ANSI Standard C library string functions.
*/
_CRTIMP void* __cdecl __MINGW_NOTHROW memchr (const void*, int, size_t) __MINGW_ATTRIB_PURE;
_CRTIMP int __cdecl __MINGW_NOTHROW memcmp (const void*, const void*, size_t) __MINGW_ATTRIB_PURE;
_CRTIMP void* __cdecl __MINGW_NOTHROW memcpy (void*, const void*, size_t);
_CRTIMP void* __cdecl __MINGW_NOTHROW memmove (void*, const void*, size_t);
_CRTIMP void* __cdecl __MINGW_NOTHROW memset (void*, int, size_t);
_CRTIMP char* __cdecl __MINGW_NOTHROW strcat (char*, const char*);
_CRTIMP char* __cdecl __MINGW_NOTHROW strchr (const char*, int) __MINGW_ATTRIB_PURE;
_CRTIMP int __cdecl __MINGW_NOTHROW strcmp (const char*, const char*) __MINGW_ATTRIB_PURE;
_CRTIMP int __cdecl __MINGW_NOTHROW strcoll (const char*, const char*); /* Compare using locale */
_CRTIMP char* __cdecl __MINGW_NOTHROW strcpy (char*, const char*);
_CRTIMP size_t __cdecl __MINGW_NOTHROW strcspn (const char*, const char*) __MINGW_ATTRIB_PURE;
_CRTIMP char* __cdecl __MINGW_NOTHROW strerror (int); /* NOTE: NOT an old name wrapper. */

_CRTIMP size_t __cdecl __MINGW_NOTHROW strlen (const char*) __MINGW_ATTRIB_PURE;
_CRTIMP char* __cdecl __MINGW_NOTHROW strncat (char*, const char*, size_t);
_CRTIMP int __cdecl __MINGW_NOTHROW strncmp (const char*, const char*, size_t) __MINGW_ATTRIB_PURE;
_CRTIMP char* __cdecl __MINGW_NOTHROW strncpy (char*, const char*, size_t);
_CRTIMP char* __cdecl __MINGW_NOTHROW strpbrk (const char*, const char*) __MINGW_ATTRIB_PURE;
_CRTIMP char* __cdecl __MINGW_NOTHROW strrchr (const char*, int) __MINGW_ATTRIB_PURE;
_CRTIMP size_t __cdecl __MINGW_NOTHROW strspn (const char*, const char*) __MINGW_ATTRIB_PURE;
_CRTIMP char* __cdecl __MINGW_NOTHROW strstr (const char*, const char*) __MINGW_ATTRIB_PURE;
_CRTIMP char* __cdecl __MINGW_NOTHROW strtok (char*, const char*);
_CRTIMP size_t __cdecl __MINGW_NOTHROW strxfrm (char*, const char*, size_t);

#ifndef __STRICT_ANSI__
/*
* Extra non-ANSI functions provided by the CRTDLL library
*/
_CRTIMP char* __cdecl __MINGW_NOTHROW _strerror (const char *);
_CRTIMP void* __cdecl __MINGW_NOTHROW _memccpy (void*, const void*, int, size_t);
_CRTIMP int __cdecl __MINGW_NOTHROW _memicmp (const void*, const void*, size_t);
_CRTIMP char* __cdecl __MINGW_NOTHROW _strdup (const char*) __MINGW_ATTRIB_MALLOC;
_CRTIMP int __cdecl __MINGW_NOTHROW _strcmpi (const char*, const char*);
_CRTIMP int __cdecl __MINGW_NOTHROW _stricmp (const char*, const char*);
_CRTIMP int __cdecl __MINGW_NOTHROW _stricoll (const char*, const char*);
_CRTIMP char* __cdecl __MINGW_NOTHROW _strlwr (char*);
_CRTIMP int __cdecl __MINGW_NOTHROW _strnicmp (const char*, const char*, size_t);
_CRTIMP char* __cdecl __MINGW_NOTHROW _strnset (char*, int, size_t);
_CRTIMP char* __cdecl __MINGW_NOTHROW _strrev (char*);
_CRTIMP char* __cdecl __MINGW_NOTHROW _strset (char*, int);
_CRTIMP char* __cdecl __MINGW_NOTHROW _strupr (char*);
_CRTIMP void __cdecl __MINGW_NOTHROW _swab (const char*, char*, size_t);

#ifdef __MSVCRT__
_CRTIMP int __cdecl __MINGW_NOTHROW _strncoll(const char*, const char*, size_t);
_CRTIMP int __cdecl __MINGW_NOTHROW _strnicoll(const char*, const char*, size_t);
#endif

#ifndef _NO_OLDNAMES
/*
* Non-underscored versions of non-ANSI functions. They live in liboldnames.a
* and provide a little extra portability. Also a few extra UNIX-isms like
* strcasecmp.
*/
_CRTIMP void* __cdecl __MINGW_NOTHROW memccpy (void*, const void*, int, size_t);
_CRTIMP int __cdecl __MINGW_NOTHROW memicmp (const void*, const void*, size_t);
_CRTIMP char* __cdecl __MINGW_NOTHROW strdup (const char*) __MINGW_ATTRIB_MALLOC;
_CRTIMP int __cdecl __MINGW_NOTHROW strcmpi (const char*, const char*);
_CRTIMP int __cdecl __MINGW_NOTHROW stricmp (const char*, const char*);
__CRT_INLINE int __cdecl __MINGW_NOTHROW strcasecmp (const char*, const char *);
__CRT_INLINE int __cdecl __MINGW_NOTHROW
strcasecmp (const char * __sz1, const char * __sz2)
return _stricmp (__sz1, __sz2);
_CRTIMP int __cdecl __MINGW_NOTHROW stricoll (const char*, const char*);
_CRTIMP char* __cdecl __MINGW_NOTHROW strlwr (char*);
_CRTIMP int __cdecl __MINGW_NOTHROW strnicmp (const char*, const char*, size_t);
__CRT_INLINE int __cdecl __MINGW_NOTHROW strncasecmp (const char *, const char *, size_t);
__CRT_INLINE int __cdecl __MINGW_NOTHROW
strncasecmp (const char * __sz1, const char * __sz2, size_t __sizeMaxCompare)
return _strnicmp (__sz1, __sz2, __sizeMaxCompare);
_CRTIMP char* __cdecl __MINGW_NOTHROW strnset (char*, int, size_t);
_CRTIMP char* __cdecl __MINGW_NOTHROW strrev (char*);
_CRTIMP char* __cdecl __MINGW_NOTHROW strset (char*, int);
_CRTIMP char* __cdecl __MINGW_NOTHROW strupr (char*);
#ifndef _UWIN
_CRTIMP void __cdecl __MINGW_NOTHROW swab (const char*, char*, size_t);
#endif /* _UWIN */
#endif /* _NO_OLDNAMES */

#endif /* Not __STRICT_ANSI__ */

#ifndef _WSTRING_DEFINED
/*
* Unicode versions of the standard calls.
* Also in wchar.h, where they belong according to ISO standard.
*/
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcscat (wchar_t*, const wchar_t*);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcschr (const wchar_t*, wchar_t);
_CRTIMP int __cdecl __MINGW_NOTHROW wcscmp (const wchar_t*, const wchar_t*);
_CRTIMP int __cdecl __MINGW_NOTHROW wcscoll (const wchar_t*, const wchar_t*);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcscpy (wchar_t*, const wchar_t*);
_CRTIMP size_t __cdecl __MINGW_NOTHROW wcscspn (const wchar_t*, const wchar_t*);
/* Note: _wcserror requires __MSVCRT_VERSION__ >= 0x0700. */
_CRTIMP size_t __cdecl __MINGW_NOTHROW wcslen (const wchar_t*);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcsncat (wchar_t*, const wchar_t*, size_t);
_CRTIMP int __cdecl __MINGW_NOTHROW wcsncmp(const wchar_t*, const wchar_t*, size_t);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcsncpy(wchar_t*, const wchar_t*, size_t);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcspbrk(const wchar_t*, const wchar_t*);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcsrchr(const wchar_t*, wchar_t);
_CRTIMP size_t __cdecl __MINGW_NOTHROW wcsspn(const wchar_t*, const wchar_t*);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcsstr(const wchar_t*, const wchar_t*);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcstok(wchar_t*, const wchar_t*);
_CRTIMP size_t __cdecl __MINGW_NOTHROW wcsxfrm(wchar_t*, const wchar_t*, size_t);

#ifndef __STRICT_ANSI__
/*
* Unicode versions of non-ANSI string functions provided by CRTDLL.
*/

/* NOTE: _wcscmpi not provided by CRTDLL, this define is for portability */
#define _wcscmpi _wcsicmp

_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wcsdup (const wchar_t*);
_CRTIMP int __cdecl __MINGW_NOTHROW _wcsicmp (const wchar_t*, const wchar_t*);
_CRTIMP int __cdecl __MINGW_NOTHROW _wcsicoll (const wchar_t*, const wchar_t*);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wcslwr (wchar_t*);
_CRTIMP int __cdecl __MINGW_NOTHROW _wcsnicmp (const wchar_t*, const wchar_t*, size_t);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wcsnset (wchar_t*, wchar_t, size_t);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wcsrev (wchar_t*);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wcsset (wchar_t*, wchar_t);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wcsupr (wchar_t*);

#ifdef __MSVCRT__
_CRTIMP int __cdecl __MINGW_NOTHROW _wcsncoll(const wchar_t*, const wchar_t*, size_t);
_CRTIMP int __cdecl __MINGW_NOTHROW _wcsnicoll(const wchar_t*, const wchar_t*, size_t);
#if __MSVCRT_VERSION__ >= 0x0700
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wcserror(int);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW __wcserror(const wchar_t*);
#endif
#endif

#ifndef _NO_OLDNAMES
/* NOTE: There is no _wcscmpi, but this is for compatibility. */
int __cdecl __MINGW_NOTHROW wcscmpi (const wchar_t * __ws1, const wchar_t * __ws2);
__CRT_INLINE int __cdecl __MINGW_NOTHROW
wcscmpi (const wchar_t * __ws1, const wchar_t * __ws2)
return _wcsicmp (__ws1, __ws2);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcsdup (const wchar_t*);
_CRTIMP int __cdecl __MINGW_NOTHROW wcsicmp (const wchar_t*, const wchar_t*);
_CRTIMP int __cdecl __MINGW_NOTHROW wcsicoll (const wchar_t*, const wchar_t*);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcslwr (wchar_t*);
_CRTIMP int __cdecl __MINGW_NOTHROW wcsnicmp (const wchar_t*, const wchar_t*, size_t);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcsnset (wchar_t*, wchar_t, size_t);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcsrev (wchar_t*);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcsset (wchar_t*, wchar_t);
_CRTIMP wchar_t* __cdecl __MINGW_NOTHROW wcsupr (wchar_t*);
#endif /* Not _NO_OLDNAMES */

#endif /* Not strict ANSI */

#define _WSTRING_DEFINED
#endif /* _WSTRING_DEFINED */

#ifdef __cplusplus

#endif

#endif /* Not RC_INVOKED */

#endif /* Not _STRING_H_ */

请问C语言对文件的读取都有哪些函数,都有啥功能?像fseek();fscanf();fread......啥的

C语言文件操作函数

13.1C语言文件
1,两种文件存取方式(输入,输出方式)
顺序存取
直接存取
2,数据的两种存放形式
文本文件
二进制文件

13.2文件指针
定义文件类型指针变量的一般形式:
FILE *指针变量名;
例如:
FILE *fp1,*fp2;

13.3打开文件
在使用文件之前,需打开文件.在C里使用fopen函数打开文件.格式为:
fopen(文件名,文件使用方式);
此函数返回一个指向FILE类型的指针.如:
FILE *fp;
fp=fopen("file_1","r");
如果调用成功,fp就指向file_1,否则返回为NULL,所以为了保证文件的正确使用,要进行测试.采用如下语句:
If((fp=fopen("file_1","r"))==NULL)

printf("Cannot open this file\n");
exit(0);

最常用的文件使用方式及其含义如下:
1,"r".为读而打开文本文件.(不存在则出错)
2,"rb".为读而打开二进制文件.
3,"w".为写而打开文本文件.(若不存在则新建,反之,则从文件起始位置写,原内容将被覆盖)
4,"wb".为写而打开二进制文件.
5,"a".为在文件后面添加数据而打开文本文件.(若不存在,则新建;反之,在原文件后追加)
6,"ab".为在文件后面添加数据而打开一个二进制文件.
最常用的文件使用方式及其含义如下:
7,"r+".为读和写而打开文本文件.(读时,从头开始;在写数据时,新数据只覆盖所占的空间,其后不变)
8,"rb+".为读和写而打开二进制文件.只是在随后的读写时,可以由位置函数设置读和写的起始位置.
9,"w+".首先建立一个新文件,进行写操作,随后可以从头开始读.(若文件存在,原内容将全部消失)
10,"wb+".功能与"w+"同.只是在随后的读写时,可以由位置函数设置读和写的起始位置.
最常用的文件使用方式及其含义如下:
11,"a+".功能与"a"相同;只是在文件尾部添加新的数据后,可以从头开始读.
12,"ab+".功能与"a+"相同;只是在文件尾部添加新数据之后,可以由位置函数设置开始读的起始位置.

13.4关闭文件

当文件的读写操作完成之后,使用fclose函数关闭文件.格式如下:
fclose(文件指针)
如:fclose(fp);

13.5调用getc(fgetc)和putc(fputc)函数进行输入和输出
1,调用putc(或fputc)函数输出一个字符
调用形式为:
putc(ch,fp);
功能是:将字符ch写到文件指针fp所指的文件中去.当输出成功,putc函数返回所输出的字符;否则,返回一个EOF值.EOF是在stdio.h库函数文件中定义的符号常量,其值等于-1.

13.5调用getc(fgetc)和putc(fputc)函数进行输入和输出
例如:把从键盘输入的文本按原样输出到名为file_1.dat文件中,用字符@作为键盘输入结束标志.

#include
Void main()

FILE *fpout;
char ch;
if(fpout=fpopen("file_1","w")==NULL)

printf("Cannot open this file!\n");
exit(0);

ch=getchar();
while(ch!='@')
fputc(ch,fpout); ch=getchar();
fclose(fpout);

2.调用getc(或fgetc)函数输入一个字符
调用形式为:
ch=getc(pf);
功能是:从pf指定的文件中读如一个字符,并把它作为函数值返回.
例如:把一个已存在磁盘上的file_1.dat文本文件中的内容,原样输出到终端屏幕上.
#include
void main()
FILE *fpin;
char ch;
if((fpin=fopen("file_1.dat","r"))==NULL)
printf("Cann't open this file!\n");exit(0);
ch=fgetc(fpin);
while (ch!=EOF)
putchar(ch); ch=fgetc(fpin);
fclose(fpin);


13.6判断文件结束函数feof
EOF可以作为文本文件的结束 标志,但不能作为二进制文件的结束符.feof函数既可以判断二进制文件,又可以判断文本文件.
例:编写程序,用于把一个文本文件(源)复制到另一个文件(目的)中,源文件名和目的文件名由命令行输入.命令形式如下:
可执行程序名 源文件名 目的文件名
#include
void filecopy(FILE* ,FILE *);
void main(int argc,char *argv[])
FILE *fpin,*fpout;
if(argc==3)
fpin=fopen(argv[1],"r");
fpout=fopen(argv[2],"w");
filecopy(fpin,fpout);
fclose(fpin);fclose(fpout);

else if(argc>3)
printf("The file names too many!!\n";
else
printf("There are no file names for input or output!!\n );

void filecopy(FILE *fpin,FILE *fpout)

char ch;
ch=getc(fpin);
while(!feof(fpin))
putc(ch,fpout); ch=getc(fpin);


13.7fscanf函数和fprintf函数
1,fscanf函数
fscanf只能从文本文件中按格式输入,和scanf函数相似,只不过输入的对象是磁盘上文本文件中的数据.调用形式为:
fscanf(文件指针,格式控制字符串,输入项表)
例如:fscanf(fp,"%d%d",&a,&b);
fscanf(stdin,"%d%d",&a,&b);
等价于scanf("%d%d",&a,&b);
3.fprintf函数
fprintf函数按格式将内存中的数据转换成对应的字符,并以ASCII代码形式输出到文本文件中.Fprintf函数和printf函数相似,只是将输出的内容按格式存放到磁盘的文本文件中.调用形式如下:
fprintf(文件指针,格式控制字符串,输出项表)
如:fprintf(fp,"%d %d",x,y);
以下语句 fprintf(stdout,"%d %d",x,y)

13.8fgets函数和fputs函数
1,fgets函数
fgets函数用来从文件中读入字符串.调用形式如下:
fgets(str,n,fp);
函数功能是:从fp所指文件中读入n-1个字符放入str为起始地址的空间内;如果在未读满n-1个字符时,则遇到换行符或一个EOF结束本次读操作,并已str作为函数值返回.
13.8fgets函数和fputs函数
2,fputs函数
fput函数把字符串输出到文件中.函数调用形式如下:
fputs(str,fp);
注意:为了便于读入,在输出字符串时,应当人为的加诸如"\n"这样的字符串.

#include <stdio.h>
#include <iostream>

int main(int argc, char *argv[])

char arr[10] ;
char *ap = "hello!" ;
FILE *fp ;
if ((fp = fopen("hello.txt", "wt+")) == NULL)

printf("error!") ;
exit(1) ;

fputs(ap, fp) ;
rewind(fp) ; //
fgets(arr, 10, fp) ;
printf("%s\n", arr) ;
fclose(fp) ;
return 0 ;


13.9fread函数和fwrite函数
例如有如下结构体:
struct st
char num[8];
float mk[5];
pers[30];
以下循环将把这30个元素中的数据输出到fp所指文件中.
for(i=0;i<30;i++)
fwrite(&pers[i],sizeof(struct st),1,fp);

13.9fread函数和fwrite函数
以下语句从fp所指的文件中再次将每个学生数据逐个读入到pers数组中.
i=0;
fread(&pers[i],sizeof(struct st),1,fp);
while(!feof(fp))
i++;
fread(&pers[i],sizeof(struct st),1,fp);


13.10文件定位函数
1,fseek函数
fseek函数用来移动文件位置指针到指定的位置上,接着的读或写操作将从此位置开始.函数的调用形式如下:
fseek(pf,offset,origin)
pf:文件指针
offset:以字节为单位的位移量,为长整形.
origin:是起始点,用来指定位移量是以哪个位置为基准的.
1,fseek函数
位移量的表示方法
标识符 数字 代表的起始点
SEEK_SET 0 文件开始
SEEK_END 2 文件末尾
SEEK_CUR 1 文件当前位置
假设pf已指向一个二进制文件,则;
fseek(pf,30L,SEEK_SET)
fseek(pf,-10L*sizeof(int),SEEK_END)
对于文本文件,位移量必须是0;如:
fseek(pf,0L,SEEK_SET)
fseek(pf,0L,SEEK_END)
2. ftell函数
ftell函数用以获得文件当前位置指针的位置,函数给出当前位置指针相对于文件开头的字节数.如;
long t;
t=ftell(pf);
当函数调用出错时,函数返回-1L.
我们可以通过以下方式来测试一个文件的长度:
fseek(fp,0L,SEEK_END);

t=ftell(fp);
3.rewind函数
调用形式为:
rewind(pf);
函数没有返回值.函数的功能是使文件的位置指针回到文件的开头.

13.10文件应用
在磁盘上的test.txt文件中放有10个不小于2的正整数,用函数调用方式编写程序.要求实现:
1,在被调函数prime中,判断和统计10个整数中的素数以及个数.
2,在主函数中将全部素数追加到磁盘文件test.txt的尾部,同时输出到屏幕上.
#include
#include
Int prime(int a[],int n)

int I,j,k=0,flag=0;
for(i=0;i for(j=2;j if(a[i]%j==0)
flag=0; break;
else flag=1;
if(flag)
a[k]=a[i];k++;

return k;

void main()
int n,I,a[10];
FILE *fp;
fp=fopen("test1-2.txt","r+");
for(n=0;n<10;n++)
fscanf(fp,"%d",&a[n]);
n=prime(a,n);
fseek(fp,o,2);
for(i=0;i printf("%3d",a[i]);
fprintf(fp,"%3d",a[i]);

fclose(fp);
参考技术A 答案在 msdn

以上是关于C语言关于字符串的操作函数都有哪些?的主要内容,如果未能解决你的问题,请参考以下文章

c语言的函数都有哪些??????

字符串比较函数都有哪些

php mysql转义特殊字符的函数都有哪些

c语言中操作符和标识符都有哪些区别和联系

C语言中的标准函数都有哪些

C语言中的标准函数都有哪些?