这是我的代码,输出的每个字母都被打印两次。它不是文本文件,它只在我插入我的putchar(tolower)
语句时发生,但它的格式与它应该的完全一致。声明有什么问题?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_STRING_SIZE 20
#define MAX_LIST_SIZE 50
int readFile(char *filename); /* function declaration for readFile, defined below */
void punct();
/* main function */
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("%s: usage %s textFileName \n", argv[0], argv[0]);
exit(1);
}
readFile(argv[1]);
return 0;
}
int readFile(char *filename) {
char ch;
FILE *fPtr;
fPtr = fopen(filename, "r"); /*open file filename, r is read only */
if (!fPtr) {
return 0;
}
while ((ch = fgetc(fPtr)) != EOF) {
putchar(tolower(ch));
printf("%c", ch);
}
fclose(fPtr);
return 1; /* because success */
}