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

测试.txt

testfileisitozetoPıorkgldstartfldsfslf
1lsfslHkf12e43Y54465kds2cmSb3cmb4 op3I3533 
5cmkr3rCdqe22e43S5446T5ztop5U6l271Rlr2l83KlccSck49
kr3rdWqe2I2e4354N465Sop33E533tC
VtteEe5R56l271Tlr2l83IlcMcSck4E9stopCCCINCISIweklemfkfKER

主文件

fldsfslf
1lsfslHkf12e43Y54465kds2cmSb3cmb4 op3I3533 
5cmkr3rCdqe22e43S5446T5ztop5U6l271Rlr2l83KlccSck49
kr3rdWqe2I2e4354N465Sop33E533tC
VtteEe5R56l271Tlr2l83IlcMcSck4E9
4

3 回答 3

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 回答