當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C語言 putc()用法及代碼示例

如果您在應用程序中處理文件,那麽將一個文件複製到另一個文件或從同一個文件複製將會遇到一些問題。因此,使用函數putc()會幫助你做到這一點。

該函數用於在文件中寫入單個字符。該函數隻需要兩個參數,第一個是字符,第二個是文件本身。

例:

    putc('a', FILE);
    //Here, FILE is the file variable.
    
    Output:
    It will write 'a' on the file at the current 
    position of the cursor.

stdio.h - C 中的 putc() 函數示例

#include <stdio.h>

int main ()
{
    // initializing the type of variables
    FILE *F;
    int c;

    // opening the file in write mode
    F = fopen("abc.txt", "w");
    for( c = 33 ; c <= 100; c++ ) {
        putc(c, F);
    }
    fclose(F);


    // opening the file in read mode
    F = fopen("abc.txt","r");
    while(1) {
        c = fgetc(F);
        if( feof(F) ) {
            break ;
        }
        // printing on console
        printf("%c", c);
    }
    
    fclose(F);

    return(0);
}

輸出

stdio.h - putc() in c language

文件內容

stdio.h - putc() in c language




相關用法


注:本文由純淨天空篩選整理自Abhishek Sharma大神的英文原創作品 putc() function of stdio.h in C。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。