2013-10-30 55 views
0

这是一段代码,其中我将带有​​空格的字符串从文本打印到另一个txt文件中。我有一个codelist,我必须用正确的代码来切换特定的字符串。代码在一个数组中。我不能使编码功能工作。 Fprintf打印代码,然后打印的基本字符串。我想跳过这些字符串。我只需要打印代码。我在哪里错过了什么?C 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

不要使用('F')'scanf'阅读字符串(但如果你真的要,指定长度)。改用['fgets'](http://en.cppreference.com/w/c/io/fgets)。 – Kninnug

+0

你的'代码'包含额外的'%'符号吗? – usr2564301

+0

它只包含英文字母表中的小写字母,但是nvm已经解决了! – Daniel

回答

1

继续是问题。

它不是继续for循环。

这是我认为应该是:

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); 
    } 
} 
+1

我爱你,真是太棒了! – Daniel