0

(これは tcsh の C、Unix 用です。)

文字列を外部ファイル (出力) に書き込んでから、出力をファイル入力と比較しようとしています。出力がそのファイルに存在する場合、「レコードが見つかりました」と出力したいと思います。そのファイルに出力が存在しない場合は、「レコードが見つかりません」と出力します。

出力と入力を比較するために while ループを使用しています。レコードが見つかった場合にループが終了し、「レコードが見つかりました」と出力するビットが機能しています。

「else」部分が機能しません。これについては、コード内の私のコメントを参照してください。

テキスト、メモ、グーグルを 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;

0 は c では偽です。したがって、上部でループしている場合

while(!found)

true と評価されます。EOF でループを終了していないため、それがファイルにない場合、出力が期待どおりに正確にファイルにない場合は、永遠にループします。

他のニュースとしては、バッファーと C でバッファーをどのように割り当てるかについて、実際に考慮する必要があります。

于 2012-03-19T20:53:29.333 に答える
0

これ:

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

foundすでに 0この時点にあり、ループ条件がであるため、効果はありません。while (!found)これは、「whilefoundが非ゼロではない」、つまり「whilefoundがゼロである」ことを意味します。

しかし、それは結構です。本当の問題は、次のことです。

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");
}

追加するために編集:上記のマヘシュのコメントを見たところですが、これは良い点です。次の 2 行も変更する必要があります。

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 に答える