0

以下の問題の主な説明、それが発生する場所。しかし、単純に、尋ねた後にエラーメッセージが表示される理由がわかりません

if (outf!=NULL){
    printf("Output file already exists, overwrite (y/n):");
    scanf("%c",yn);
}

outf は、既存のファイルへのファイル ポインタです。コードの途中で説明を読んでください。

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

int main() {

/* Declare file pointer */
    FILE *inf;
    FILE *outf;

    int linenumber,linecounter=0,linepresent;
    char filename[21];
    char detail[21];
    char linedetail[21];
    char outfilename[21];
    char letter,yn='y';
    int position;

/*INPUT DETAILS Ask user for file name and line number*/

    printf("Please enter an input filename and a linenumber: ");
//scan filename to char string and line number to int variable
    scanf("%s %i",&filename,&linenumber);

/*OUTPUT DETAILS Ask user for file name, letter & position, etc*/
    printf("Please enter an output filename, a letter and a position:");    
    scanf("%s %c %i",&outfilename,&letter,&position);

/* Open file for reading */
    inf=fopen (filename,"r");
    outf=fopen(outfilename,"r");
/*check that file exists*/
    if (inf!=NULL) {

ここまではすべて正常に動作します。次に、outf ファイルが既に存在するかどうかを調べます。outf が既存のファイルを指している場合、「出力ファイルは既に存在します。上書きします (y/n):」と出力します。

ただし、これを印刷するとすぐに、エラーウィンドウが開きます! これはおそらく非常に初歩的な間違いです。私はまだ C を学んでいます。そのようなファイルがない場合、プログラムは正常に完了し、if ステートメントをバイパスします。

        if (outf!=NULL){
            printf("Output file already exists, overwrite (y/n):");
            scanf("%c",yn);
        }
        if (yn=='y'){
    /*keep reading to end of file*/
            while (feof(inf)==0) {
                linecounter++;
    /*read each line and store the line number in detail[WORDS GO HERE]*/
                fscanf (inf,"%s", &detail);
    /*If we reach the line selected by the user*/
                if (linecounter==linenumber){
                    strcpy(linedetail,detail);
                    linepresent=1;
                }
            }
            if (linepresent==0) {
                printf("File only contains %i lines",linecounter);
            }
        } else {
            exit(1);
        }
    } else {
        printf("Input file not found");
    }

printf("%s",linedetail);

/* close the file */

    fclose(inf);
    fclose(outf);

    return (0);

}
4

3 に答える 3

3

まず、既に述べた問題: 出力ファイルを読み取りモードで開いています。書き込み用に開くには:

outf=fopen(outfilename,"w");  /* Note the "w". */

また、scanf() は、変数の値ではなく、変数へのポインターを受け入れるため、 と記述scanf("%c", yn);すると、scanf に文字yをポインターとして渡すことになりますが、これはナンセンスです。次のようにする必要がありますscanf("%c", &yn);

ただし、これらを修正しても、プログラムは期待どおりに動作しません。書き込み用に開こうとしているファイルが存在せず、fopen()NULL を返さない場合、新しいファイルが作成されます。出力ファイルが存在する場合、コードは常に出力ファイルを上書きします。NULL はfopen、ファイルを開いたり作成したりできなかった場合 (たとえば、それを行う権限がなかった場合) にのみ返され、次のように処理する必要があります。

outf=fopen(outfilename, "w");
if(outf == NULL) {
    perror("Failed to open output file: ");
    fclose(inf);  /* Don't leave files opened. It's bad form. */
    exit(1);
}
/* Open succeeded, do your stuff here. */

プログラムはすぐに終了するため、elseの後にブロックは必要ないことに注意してください。ifexit()

また、「ファイルへのポインタ」のようなものはありません。FILE は、開いているファイルを表す単なる構造です。

于 2011-12-10T18:24:54.100 に答える
0

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

読み取りフラグを使用して出力ファイルを開いています。「w」に変えてみてください。

outf=fopen(outfilename,"w");

ただし、「w」で開いたファイルに書き込むと、古いファイルが壊れてしまうことに注意してください。「a」を使用してファイルに追加します。

于 2011-12-10T18:02:19.020 に答える
0

関数のアドレスを渡す必要がありynますscanf

scanf("%c", &yn);

于 2011-12-10T18:03:08.070 に答える