C版 :
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
int main(int argc,char **argv) {
FILE *input = fopen("in.test.txt","r");
char c = '\0';
while (! feof(input)) {
fscanf(input,"%c",&c);
printf("%c",tolower(c));
}
printf("\b\n");
return 0;
}
/*
* in.test.txt: "XXX"
* OutPut: "xxxx"
*/
C++版 :
#include <cstdio>
#include <cerrno>
#include <cctype>
using namespace std;
int main(int argc,char **argv) {
FILE *input = fopen("in.test.txt","r");
char c = '\0';
while (! feof(input)) {
fscanf(input,"%c",&c);
printf("%c",tolower(c));
}
printf("\b\n");
return 0;
}
/*
* in.test.txt: "XXX"
* OutPut: "xxxx"
*/
求助大佬们为什么会最后一个字符输出两次?
#include <cstdio>
#include <cerrno>
#include <cctype>
using namespace std;
int main(int argc,char **argv) {
FILE *input = fopen("c:\\in.test.txt","r");
char c = '\0';
while (1) {
if (fscanf(input,"%c",&c) == EOF) break;
printf("%c",tolower(c));
}
printf("\b\n");
return 0;
}
scanf失败,c还是之前的值,所以多输出1个字符。