当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。