小写字符在C和大写字符;写入文件 [英] Lowercase characters to Uppercase characters in C & writing to file

查看:183
本文介绍了小写字符在C和大写字符;写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的内容读入C中的字符数组我怎么会改变所有的文件中是小写字母为大写字母的信吗?

I am reading content from a file to be read into a char array in C. How could I change all the letters in the file that are lowercase to uppercase letters?

推荐答案

下面是一个可能的算法:

Here's a possible algorithm:


  1. 打开一个文件(我们称之为A) - fopen()函数

  2. 打开另一个文件中写入(我们称之为B) - fopen()函数

  3. 阅读A的含量 - GETC()或FREAD();不管你觉得自由

  4. 请您阅读大写的内容 - TOUPPER()

  5. 写了4步到B的结果 - FWRITE()或fputc()函数或fprintf中()

  6. 关闭所有文件句柄 - FCLOSE()

下面是用C语言编写的code:

The following is the code written in C:

#include <stdio.h>
#include <ctype.h>

#define INPUT_FILE      "input.txt"
#define OUTPUT_FILE     "output.txt"

int main()
{
    // 1. Open a file
    FILE *inputFile = fopen(INPUT_FILE, "rt");
    if (NULL == inputFile) {
        printf("ERROR: cannot open the file: %s\n", INPUT_FILE);
        return -1;
    }

    // 2. Open another file
    FILE *outputFile = fopen(OUTPUT_FILE, "wt");
    if (NULL == inputFile) {
        printf("ERROR: cannot open the file: %s\n", OUTPUT_FILE);
        return -1;
    }

    // 3. Read the content of the input file
    int c;
    while (EOF != (c = fgetc(inputFile))) {
        // 4 & 5. Capitalize and write it to the output file
        fputc(toupper(c), outputFile);
    }

    // 6. Close all file handles
    fclose(inputFile);
    fclose(outputFile);

    return 0;
}

这篇关于小写字符在C和大写字符;写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆