在文件c中写入

在文件c中写入,c,C,所以基本上在下面的程序中,我尝试将文本的大写字母转换为小写字母,将小写字母转换为大写字母,并将结果打印到文件中…但运行程序后,我得到的唯一结果是一系列数字。。。我试图将fprintf函数“%s”作为第二个参数,但没有得到更好的结果 #include <stdio.h> #include <ctype.h> int main( ) { char c; int charToLowerCase = 0; int charToUpperCase =

所以基本上在下面的程序中,我尝试将文本的大写字母转换为小写字母,将小写字母转换为大写字母,并将结果打印到文件中…但运行程序后,我得到的唯一结果是一系列数字。。。我试图将fprintf函数“%s”作为第二个参数,但没有得到更好的结果

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

int main( )

{
    char c;

    int charToLowerCase = 0;
    int charToUpperCase = 0;
    int countCharacters = 0;
    FILE *in_file;
    FILE *out_file;
    in_file = fopen("input.txt", "r");
    out_file = fopen("output.txt", "w");
    c = fgetc(in_file);
    while (c != EOF)
    {
        if (c >= 'A' && c <= 'Z')
        {
            fprintf(out_file, "%d", tolower(c));
            charToLowerCase++;
        }
        else if (c >= 'a' && c <= 'z')
        {
            fprintf(out_file, "%d", toupper(c));
            charToUpperCase++;
        }
        else
            fprintf(out_file, "%d", c);

        c = fgetc(in_file);
        countCharacters++;
    }
    fclose(in_file);
    fclose(out_file);
    return 0;
}
#包括
#包括
int main()
{
字符c;
int charToLowerCase=0;
int-charToUpperCase=0;
int countCharacters=0;
文件*在_文件中;
文件*输出文件;
in_file=fopen(“input.txt”,“r”);
out_file=fopen(“output.txt”,“w”);
c=fgetc(在文件中);
而(c!=EOF)
{

如果(c>='A'&&c='A'&&c
tolower
toupper
返回一个
int
(这样他们就可以处理EOF和其他异常)

您几乎可以安全地转换为
字符
(严格地说,您应该检查返回的
int
)并使用适当的格式化程序,对于
字符
,它是
%c


另外,请注意,C标准允许在小写和大写字母不一定在连续块中的情况下使用编码方案。请参见EBCDIC。严格地说,如果(C>='A'&&C在
fprintf
语句中使用
%C
%d
是整数


@RalucaDamarisLupeş-太棒了!@RalucaDamarisLupeş:别忘了将这个答案标记为已接受-它与我的答案不同,直截了当。最好使用
int c
,而不是
char c
。它是由
fgetc()
tolower()
toupper()返回的类型
和它的正值是
tolower()
toupper()
所期望的值。同意。只要你还在,也没有理由在
fputc
足够时使用
fprintf
大锤。如果(c>='A'&&c绝对正确!这就是为什么我也保持沉默的原因。一个可移植的解决方案是使用字符文本作为大小写标签的开关块。