因此,基本上在下面的程序中,我嘗試將文本的大寫字母轉換爲小寫字母,將小寫字母轉換爲大寫字母並將結果打印到文件中...但運行程序後,我唯一得到的是一系列數字......我試圖把作爲第二個參數函數fprintf「%s」但我沒有得到一個更好的結果...寫入文件c
#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 C',不'焦炭C'在這種情況下。它是'fgetc()','tolower()'和'toupper()'返回的類型,其正值是'tolower()'和'toupper()'所期望的值。 – chux
同意。只要你在這裏,[使用'isupper'和'islower'](http://pastebin.com/WM3mWeqH)。當'fputc'足夠時,也沒有理由使用'fprintf'的大錘。 – WhozCraig
'if(c> ='A'&& c <='Z')'不可攜帶。 [字母不保證是連續的](https://en.wikipedia.org/wiki/EBCDIC)。每個** 5.2.1 [C標準]的字符集**(http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf):* 的值執行字符集的成員是實現定義的*只有數字保證爲連續的:*在源和執行基本字符集中, '0' 之後的每個字符的 值在上面的 列表中的十進制數字應該大於前面的值 。* –