C语言字符串与字符数组

字符串儿与字符数组

 

字符数组的定义:

Char buffer[100];

字符数组初始化:

Char buffer1[100]="hello world";

利用scanf输入一个字符串儿

代码:

#include <iostream>

int main() {
    //字符数组定义,对于C语言就是最后一个元素为\0的char数组。
    char buffer[100];
    //字符数组初始化
    char buffer1[]="hello world";
    
    printf("请输入一个字符串儿:\n");
    scanf("%s",buffer);
    printf("输入的字符串是:\n"); 
    printf(buffer);
    
    return 0;
}

 

运行结果:

 C语言字符串与字符数组

/*这里有一个小插曲:

Int a = 0;

Scanf("请输入整形a的值%d",&a);

这个 就比较坑,要输入一个,比如我们要给a赋一个5.

那么我们在控制台就要输入:

请输入整形a的值5

然后 printf("%d",a);就可以了。

*/

 

字符串儿处理函数:

gets()从外设读取字符串儿放到对应的字符串儿数组中。但是存在 内存泄露的问题,如果在第二函数里面输入过多的内容,就会导致缓冲区溢出。

溢出示意图:

 C语言字符串与字符数组

【演示图与效果图中w的数量不一致】

溢出效果图:

 C语言字符串与字符数组

演示代码:

#include <iostream>

int main() {
    
    char charArray1[10];
    char charArray2[10];

    gets(charArray1);
    printf(charArray1);
    printf(charArray2);
    gets(charArray2);
    printf(charArray1);
    printf(charArray2);
    
    return 0;
}

/*

也未必总是会报错:

C语言字符串与字符数组

这次就没出异常。然后修改了一下代码。

*/

这样系统就会很不稳定。所以就诞生了:fgets() 函数。

示例用法:

 

void fgetsFunction(void){
    
    char charArray[10];
    fgets(charArray,sizeof(charArray),stdin);
    printf(charArray);
    
}

运行结果:

 C语言字符串与字符数组

尝试冲突:

 

 C语言字符串与字符数组

 

printf() 与   puts() 的输出区别。

示例代码:

void putsFunction(void){
    char charArray[]="hello world";
    printf(charArray);
    puts(charArray);
    printf(charArray);
}

运行结果:

 

然后是fputs(),文件输出的意思:

查阅了一下文档:

【function

http://www.cplusplus.com/reference/cstdio/fputs/

<cstdio>

fputs

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

Write string to stream

Writes the C string pointed by str to the stream.
The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This terminating null-character is not copied to the stream.
Notice that fputs not only differs from puts in that the destination stream can be specified, but also fputs does not write additional characters, while puts appends a newline character at the end automatically.

简单翻译:

写一个字符串到流

写一个C的字符串到流中。

这个函数会复制 从str起始地址到str\0的位置的内容。终止符号\0并不会被复制到流中。

Fputs与 puts函数的区别在两个地方:是否会在文件结尾添加新的换行标记。文档里面这样写,说明在 windows里面是 \n\r linux 里面是 \n.另外一个区别是:fputs的目标流是被指定的。【毕竟后面带了一个参数,用以指定。这也就是为什么他们是为了文件操作而生成的函数,因为要指定对应的流对象。

/**

为了看懂另外的区别,就不得不看下面的例程:

Example

123456789101112131415 /* fputs example */

#include <stdio.h>

int main ()
{
   FILE * pFile;//声明文件
   char sentence [256];//声明字符串数组

   printf ("Enter sentence to append: ");//提示
   fgets (sentence,256,stdin);//防止内存溢出的得到一个字符串
   pFile = fopen ("mylog.txt","a");//懵逼
   fputs (sentence,pFile);
   fclose (pFile);
   return 0;
} 

然后 就要看 fopen这个 【http://www.cplusplus.com/reference/cstdio/fopen/

然后找到这个参数a

 C语言字符串与字符数组

别的应该都没啥用,那就应该是 指打开一个文件了。

 C语言字符串与字符数组

好吧,不能打眼瞅。。。为了输出而打开一个文件,在文件的末尾,输出操作通常是在文件的末尾写数据,来扩展这个文件。存储操作(fseek,fsetpos,rewind)会被忽略。如果这个文件不存在的话,就创建它。

这段示例代码的运行结果:

文件目录下有一个 

 C语言字符串与字符数组

双击打开,是执行了三次的运行结果。

 C语言字符串与字符数组

*/

其实 看到 fgets() 和 fputs() 也就应该明白是怎么回事儿了,因为 带一个 f也就应该想到是跟文件操作相关的,而他们都是防止发生,内存溢出的,在指定参数的时候就严格限定了。【看上面那行红字】