我正在尝试读取文件并打印文件中的所有单词,忽略所有其他空格和符号。我有它与strcpy工作,但它给了我一个错误,我试图使用sprintf,但我真的不明白如何功能的话。它打印随机整数而不是字符串。添加int到字符串时遇到问题,尝试使用sprintf但我遇到了问题
编辑:我对C完全陌生,所以我没有把我的指针搞得太好。
FILE *file;
file = fopen("sample_dict.txt", "r");
int c;
int wordcount = 0;
int count = 0;
const char *a[10];
char word[100];
do {
c = fgetc(file);
//error statement
if (feof(file)) {
break;
}
if (isalpha(c) && count == 2) {
printf("%s\n", word);
memset(word, 0, sizeof(word));
count = 1;
wordcount++;
}
if (isalpha(c)) {
//strcat(word, &c);
sprintf(word, "%d", c);
continue;
}
count = 2;
continue;
} while (1);
fclose(file);
return (0);
return 0;
使用'%D'就可以打印它是这样的,当打印'char'为整);'但是你重置每个字符中的单词。更好地使用计数器并复制到数组中... – SHR 2014-10-09 23:54:05