-5

test.txt という名前のファイルがあります。ファイルから一文字ずつ読みたい。次に、「開始」から「停止」まで新しいファイルの書き込みを開始します。その名前は main.txt です。コーディングしようとしましたが、実行されませんでした。私を助けてください。

#include<stdio.h>
//#include<conio.h>

FILE *fpR, *fpW;
char RFile[25],WFile[25],stArt[5],stOp[4],swA[5],swO[4];
char *c; 
int cc=0,i=0;

//  clrscr();
//Readin file's open process   
printf("Please! Enter the name of the file to be read : \n");
scanf("%s",RFile);

//Writing file's open process      
printf("Please! Enter the name of the file to be write : \n");
scanf("%s",WFile);

//Openin files
fpR = fopen(RFile,"r");
if (fpR==NULL) { 
    printf("Could not open %s!\n",RFile); 
    return 1;
}
fpW = fopen(WFile,"w");
if (fpW==NULL) { 
    printf("Could not open %s!\n",WFile); 
    return 1;
}

do {
    for(i = 1;i <= 5;i++) {
    swA[i] = fgetc(fpR);
    if (swA=="start"){
        fprintf(fpW,"%s",swA);
        fprintf(stdout,"%s",swA);   
    }
    for(i = 1;i <= 4;i++) {
    swO[i] = fgetc(fpR);
    if (swO=="stop"){
        break;  
    }  
}while (c != EOF);

// Close files
fclose(fpR);
fclose(fpW); 
//   getch();
return 0;
}

test.txt

testfileisitozetoPıorkgldstartfldsfslf
1lsfslHkf12e43Y54465kds2cmSb3cmb4 op3I3533 
5cmkr3rCdqe22e43S5446T5ztop5U6l271Rlr2l83KlccSck49
kr3rdWqe2I2e4354N465Sop33E533tC
VtteEe5R56l271Tlr2l83IlcMcSck4E9stopCCCINCISIweklemfkfKER

main.txt

fldsfslf
1lsfslHkf12e43Y54465kds2cmSb3cmb4 op3I3533 
5cmkr3rCdqe22e43S5446T5ztop5U6l271Rlr2l83KlccSck49
kr3rdWqe2I2e4354N465Sop33E533tC
VtteEe5R56l271Tlr2l83IlcMcSck4E9
4

3 に答える 3

2

2 つの文字列が等しいかどうかを確認する 代わりに、おそらくstrcmp()を使用する必要があります。==

cに対してチェックしているにもかかわらず、変数を使用していないようですEOF。そして、あなたは本当にあるcべきchar *ですか?

発生しているエラーの種類をより詳細に記述し、小さなステップでコードを記述して、バグを見つけやすくします。

于 2015-07-06T23:39:20.793 に答える
1

ここで fgets() を使用して、ファイル全体を配列に格納できます。

ここにあなたのコードのように-

do {
     for(i = 1;i <= 5;i++)
     {
        swA[i] = fgetc(fpR);
        if (swA=="start"){
        fprintf(fpW,"%s",swA);
        fprintf(stdout,"%s",swA);   
     }
    for(i = 1;i <= 4;i++) 
     {
        swO[i] = fgetc(fpR);
        if (swO=="stop"){
        break;  
     }  
}while (c != EOF);

これの代わりに fgets() を使用できます -

   #define MAX_LEN 1024
   char ch[MAX_LEN];
   while(fgets(ch,MAX_LEN,fpR))
  { 
            fprintf(fpW,"%s",ch);
  }

ここで「停止」または EOF をチェックする必要はありません。これは、fgets() 自体が EOF に遭遇すると返されるためです。

于 2015-07-07T05:23:07.027 に答える
0

strcmp()コマンドを使用してコードを変更し、*cygwin64 でコンパイルしたコードを削除しました。

私のエラーコード:

$ gcc --version
gcc (GCC) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -o mread mread.c
$ ./mread
Please! Enter the name of the file to be read :
test.txt
Please! Enter the name of the file to be write :
main.txt
Segmentation fault (core dumped)

変更されたコード:

#include<stdio.h>
//#include<conio.h>

main()
{
    FILE *fpR, *fpW;
    char RFile[25],WFile[25],stArt[5]={"start"},stOp[4]={"stop"},swA[5],swO[4];
    char c; 
    int cc=0,i=0;


//  clrscr();

    //Readin file's open process   
    printf("Please! Enter the name of the file to be read : \n");
    scanf("%s",RFile);

    //Writing file's open process      
    printf("Please! Enter the name of the file to be write : \n");
    scanf("%s",WFile);

    //Openin files
    fpR = fopen(RFile,"r");
    if (fpR==NULL) { 
        printf("Could not open %s!\n",RFile); 
        return 1;
    }
    fpW = fopen(WFile,"w");
    if (fpW==NULL) { 
        printf("Could not open %s!\n",WFile); 
        return 1;
    }

    do {
        c = fgetc(fpR);
        fprintf(stdout,"%s",c); 
        for(i = 1;i <= 5;i++) {
            c = fgetc(fpR);
            swA[i]= c;
            if (stArt==swA){
                fprintf(fpW,"%s",swA);
                fprintf(stdout,"%s",swA);   
            }
        }

        for(cc = 1;cc <= 4;cc++) {

            swO[cc] =fgetc(fpR);
            if (stArt==swO){
                break;  
            }
        }
    } while (c != EOF);

    // Close files
    fclose(fpR);
    fclose(fpW);

//   getch();

   return 0;
}
于 2015-07-07T01:17:58.703 に答える