【问题标题】:Problem with having uppercase letters for text file fscanf文本文件 fscanf 有大写字母的问题
【发布时间】:2021-12-29 21:57:57
【问题描述】:

好的,所以我一直在处理 fscanf 代码,但遇到了一个问题。那么当我们使用转换为大写时我们使用什么?我的代码在这里:

#include <stdio.h>
#include <string.h>

int main() {
FILE* input_file = fopen("file1.txt","r");

if (input_file == NULL){
    printf("Unable to open input file\n");
    return 1;
}
   
    char line[1000];
    while( fgets(line, 1000, input_file) != NULL ){
        //maybe
        int length = strlen(line);
          if (line[length-1] == '\n'){
              line[length-1] = '\0';
          }
        //
        for(int i = 0; line[i] != '\0'; i++){
            if (line[i] == '\n'){
                line[i] = '\0';
            }
        }
         printf("%c\n", toupper(line*));
    }

fclose(input_file);
input_file = NULL; 
return 0;
}

我尝试过使用 toupper 但不起作用...我拥有的文本文件是:

Tomorrow, and tomorrow, and tomorrow,
Creeps in this petty pace from day to day,
To the last syllable of recorded time;
And all our yesterdays have lighted fools
The way to dusty death. Out, out, brief candle!
Life's but a walking shadow, a poor player
That struts and frets his hour upon the stage
And then is heard no more. It is a tale
Told by an idiot, full of sound and fury
Signifying nothing.

我能够复制但我不知道如何让它大写....还有谁能告诉我你会如何输入总单词字符和字母字符?因为我想看看它是如何工作的……目前我收到的是enter image description here

【问题讨论】:

  • 同理,要求文本文件中的所有单词都变成大写字母
  • printf("%c\n", toupper([i])); 放入 for(int i...) 循环中
  • 请将错误发布为文本而不是屏幕截图。图像显示line* 上的语法错误。 toupper() 需要int,而linechar *,所以第一个字母是*lineline[0]。它的格式很差,很难阅读。您已经找到每个单词的第一个字母,然后将其添加。 strsep() 可能会有所帮助。
  • 你能告诉我你是怎么做到的吗?我很困惑

标签: c scanf


【解决方案1】:

如果您只想将输入大写,请使用 toupper() 对非字母做正确的事情(即什么都不做)这一事实:

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

#define LEN 1000

int main() {
    FILE *input_file = fopen("file1.txt","r");
    if(!input_file) {
        printf("Unable to open input file\n");
        return 1;
    }

    char line[LEN];
    while(fgets(line, LEN, input_file)) {
        for(int i = 0; line[i]; i++) {
            line[i] = toupper(line[i]);
        }
        printf("%s", line);
    }
    fclose(input_file);
    return 0;
}

【讨论】:

    猜你喜欢
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多