为什么不能编译?看不到错误
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char *c;
FILE *f = fopen("file.txt", "r");
if(f == NULL) {
printf("Could not open file");
}
while((c = fgetc(f)) != EOF) {
if(strcmp(c, " ") == 0) {
printf(" ");
} else if(strcmp(c, ":") == 0) {
printf(":");
} else if(strcmp(c, "@") == 0) {
printf("@");
} else if(strcmp(c, "\n") == 0) {
printf("\n");
} else {
printf("Not a valid char");
}
}
最佳答案
fgetc
以整数形式返回当前文件指针处的字符。
所以 char *c;
应该是 int c;
和
if(strcmp(c, " ") == 0) {
应该是
if(c == ' ') {
并类似地更改其他比较。
您可以将比较压缩为:
while((c = fgetc(f)) != EOF) {
if(c == ' ' || c == ':' || c == '@' || c == '\n') {
printf("%c",c);
} else {
printf("Not a valid char");
}
}
关于c - C中的字符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3752975/