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


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。