0

(这是用于 tcsh 中的 C、Unix 的。)

我正在尝试将字符串写入外部文件(输出),然后将我的输出与文件输入进行比较。如果该文件中存在输出,我想打印“找到记录”。如果该文件中不存在输出,我想打印“找不到记录”。

我正在使用 while 循环将输出与输入进行比较。我有一点工作,如果找到记录,循环终止并打印“找到记录”。

我无法让“其他”部分工作。请参阅我在代码中对此的评论。

48 小时以来,我一直在阅读我的文字、笔记和谷歌搜索。我似乎无法解决这个问题。

谢谢你的帮助。

    /*This program opens a file, compares output to file input*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILE_NAME "~/MyFiles/File1"

int main() {

FILE *fp;
char *input;
char *output;
output = "MyName";
input = "new name"; /*I get a compiler error if I don't initialize input*/
int found = 0;

/*Open file, write output ("MyName") to file in order to compare below*/
fp = fopen(FILE_NAME, "a+");
fprintf(fp, "%s\n", output);
fclose(fp);

/*Testing to see what it prints, not relevant to my question other than reopening the         
file to read in and compare below*/
fp = fopen(FILE_NAME, "r");
fscanf(fp, "%s", input);
printf("\n%s", input);

while (!found) {
    if (strcmp(input, "MyName") == 0) {
       printf("Record found.");
       found = 1;
   }
/*This is the part I can't get to work. I don't know what's off.*/
    else {
       printf("Record not found."); /*Printing this so I can see how many times it's 
 checking. It never terminates. How do I get it to scan through the file ONCE and then 
 stop?*/
       fscanf(fp, "%s", input);
       found = 0; /*I thought this was my loop terminator, but it has no effect.*/
   }
}

fclose(fp);
return 0;

}
4

2 回答 2

0

这个:

found = 0; /*I thought this was my loop terminator, but it has no effect.*/

没有效果,因为此时已经found是,并且因为您的循环条件是,这意味着“while不是非零”,即“while为零”。 0while (!found)foundfound

但这没关系。真正的问题是:

fscanf(fp, "%s", input);

最终会到达文件的末尾,并返回EOF,但你不检查它的返回值;因此,如果您在没有找到记录的情况下到达文件末尾,那么您永远不会退出循环。

所以,你可以改变这个:

while (!found) {
    if (strcmp(input, "MyName") == 0) {
       printf("Record found.");
       found = 1;
   }
/*This is the part I can't get to work. I don't know what's off.*/
    else {
       printf("Record not found."); /*Printing this so I can see how many times it's 
 checking. It never terminates. How do I get it to scan through the file ONCE and then 
 stop?*/
       fscanf(fp, "%s", input);
       found = 0; /*I thought this was my loop terminator, but it has no effect.*/
   }
}

对此:

while (!found) {
    if (strcmp(input, "MyName") == 0) {
       found = 1;
    } else {
       if(fscanf(fp, "%s", input) == EOF) { // end-of-file (or error)
           break;
       }
   }
}
if(found) {
   printf("Record found.\n");
} else {
   printf("Record not found.\n");
}

编辑添加:我刚刚在上面看到了 Mahesh 的评论,这是一个很好的观点。您还需要更改这两行:

char *input;

input = "new name"; /*I get a compiler error if I don't initialize input*/

像这样:

char input[1024]; /* allocate space for up to 1024 characters */

和这个:

fscanf(fp, "%s", input)

对于这样的事情:

fscanf(fp, "%1023s", input) /* read at most 1023 characters */

在这两个地方。

于 2012-03-19T20:52:44.280 回答
0
found=0;

0 在 c 中是假的。所以如果在顶部你正在循环

while(!found)

这将评估为真。由于您没有在 EOF 上退出循环,那么如果它不在文件中,如果输出不在文件中完全符合您的预期,那么您将永远循环。

在其他新闻中,您真的应该考虑缓冲区以及它们应该如何在 C 中分配。

于 2012-03-19T20:53:29.333 回答