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


C语言 putc()用法及代码示例


在 C 中,putc() 函数用于写入作为参数传递给给定流的字符。它是在 <stdio.h> 头文件中定义的标准库函数。该函数首先将字符转换为 unsigned char,然后将其写入给定流中文件指针指示的位置,最后将文件指针加 1。

putc() 的语法

int putc(int ch, FILE *stream);

参数

  • ch- 这是要写入的字符。
  • stream- 这是一个指向 FILE 对象的指针,该对象标识要写入字符的流。

返回值

  • 如果操作成功,函数返回写入的字符。
  • 如果发生错误或到达文件末尾,则返回 EOF。

Cputc() 的示例

示例 1:

在此示例中,我们将使用 putc() 将单个字符写入文件。

C


// C program to demonstrate the putc() function
#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE* fp = NULL;
    // Open the file in write mode
    fp = fopen("C:\\Users\\General\\Desktop\\file.txt",
               "w");
    if (fp == NULL) {
        // Print an error message if
        // the file couldn't be opened
        printf("The file can't be opened!\n");
        // Exit the program with an error code
        exit(1);
    }
    // Character to be written to the file
    char ch = 'A';
    // Write the character to the file
    putc(ch, fp);
    // Print a message
    // indicating that
    // the file has
    // been modified
    printf("File has been modified !\n");
    // Close the file
    fclose(fp);
    // Set the file pointer to NULL for safety
    fp = NULL;
    return 0;
}
输出
File has been modified !

如果我们打开生成的file.txt,它会打印以下内容:

C


// C program to display the content of the above text file
#include <stdio.h>
int main()
{
    FILE* fptr;
    int temp;
    // Open the file in read mode
    fptr = fopen("C:\\Users\\General\\Desktop\\file.txt",
                 "r");
    while (1) {
        // Read a character from the file
        temp = fgetc(fptr);
        // Check if end of file has been
        // reached
        if (feof(fptr)) {
            // If so, break out of the loop
            break;
        }
        // Print the character
        printf("%c", temp);
    }
    // Close the file
    fclose(fptr);
    return (0);
}

输出

A

示例 2:

在此示例中,我们将使用 for 循环将 A 到 Z 之间的所有字符写入 putc() 的文件中。请看下面的代码:

C


// C program to demonstrate the putc() function
#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE* fp = NULL;
    // Open the file in write mode
    fp = fopen("C:\\Users\\General\\Desktop\\file.txt",
               "w");
    if (fp == NULL) {
        // Print an error message if
        // the file couldn't be opened
        printf("The file can't be opened!\n");
        // Exit the program with an error code
        exit(1);
    }
    for (int ch = 65; ch <= 90; ch++) {
        // Write characters A to Z to the file
        putc(ch, fp);
    }
    // Print a message
    // indicating that
    // the file has
    // been modified
    printf("File has been modified !\n");
    // Close the file
    fclose(fp);
    // Set the file pointer to NULL for safety
    fp = NULL;
    return 0;
}
输出
File has been modified !

如果我们打开生成的file.txt,它会打印以下内容:

C


// C program to display the content of the above text file
#include <stdio.h>
int main()
{
    FILE* fptr;
    int temp;
    // Open the file in read mode
    fptr = fopen("C:\\Users\\General\\Desktop\\file.txt",
                 "r");
    while (1) {
        // Read a character from the file
        temp = fgetc(fptr);
        // Check if end of file has been reached
        if (feof(fptr)) {
            // If so, break out of the loop
            break;
        }
        // Print the character
        printf("%c", temp);
    }
    // Close the file
    fclose(fptr);
    return (0);
}

输出

ABCDEFGHIJKLMNOPQRSTUVWXYZ


相关用法


注:本文由纯净天空筛选整理自sriparnxnw7大神的英文原创作品 C Library Function – putc()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。