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;
}

 

運行結果:

 

/*這里有一個小插曲:

Int a = 0;

Scanf("請輸入整形a的值%d",&a);

這個 就比較坑,要輸入一個,比如我們要給a賦一個5.

那么我們在控制台就要輸入:

請輸入整形a的值5

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

*/

 

字符串兒處理函數:

gets()從外設讀取字符串兒放到對應的字符串兒數組中。但是存在 內存泄露的問題,如果在第二函數里面輸入過多的內容,就會導致緩沖區溢出。

溢出示意圖:

 

【演示圖與效果圖中w的數量不一致】

溢出效果圖:

 

演示代碼:

#include <iostream>

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

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

/*

也未必總是會報錯:

這次就沒出異常。然后修改了一下代碼。

*/

這樣系統就會很不穩定。所以就誕生了:fgets() 函數。

示例用法:

 

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

運行結果:

 

嘗試沖突:

 

 

 

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

 

別的應該都沒啥用,那就應該是 指打開一個文件了。

 

好吧,不能打眼瞅。。。為了輸出而打開一個文件,在文件的末尾,輸出操作通常是在文件的末尾寫數據,來擴展這個文件。存儲操作(fseek,fsetpos,rewind)會被忽略。如果這個文件不存在的話,就創建它。

這段示例代碼的運行結果:

文件目錄下有一個 

 

雙擊打開,是執行了三次的運行結果。

 

*/

其實 看到 fgets() 和 fputs() 也就應該明白是怎么回事兒了,因為 帶一個 f也就應該想到是跟文件操作相關的,而他們都是防止發生,內存溢出的,在指定參數的時候就嚴格限定了。【看上面那行紅字】

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM