以下是代码片段,其中我将带有文本空格的字符串打印到另一个txt文件中。我有一个代码清单,我必须用正确的代码切换特定的字符串。代码在一个数组中。我不能使编码功能工作。 Fprintf打印代码,后跟基本字符串。我想跳过这些字符串。我只需要打印代码。我在哪里想念什么?
int m;
file = fopen("input.txt", "r" );
while (fscanf(file, "%s", word) != EOF ) {
for (m=0; m<j; m++) {
if (strcmp(word, particularwords[m]) == 0) {
fprintf(outfile, "%s ", code[m]);
continue;
}
}
fprintf(outfile, "%s ", word);
}
答案 0 :(得分:1)
继续是问题。
它继续for循环而不是while。
我认为应该是这样:
int m;
file = fopen("input.txt", "r" );
while (fscanf(file, "%s", word) != EOF ) {
for (m=0; m<j; m++) {
if (strcmp(word, particularwords[m]) == 0) {
fprintf(outfile, "%s ", code[m]);
break; //for
}
}
if(m==j){ //word not found!
fprintf(outfile, "%s ", word);
}
}