-5

I have a file whose name is test.txt. I want to read character by character from the file. Then start writing from "start" to "stop" a new file, its name is main.txt. I tried to code, however it did not run. Please help me.

#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
bdllhtlgn
  • 31
  • 7
  • How did it "not run"? Do you get errors/what happens? – sth Jul 06 '15 at 23:25
  • I think I have error in this part `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);` – bdllhtlgn Jul 06 '15 at 23:27
  • why don't you either load the whole file into an array and search for "start" and "stop" use the indices to write the desired data range to another file using "fputc". – jdl Jul 06 '15 at 23:37
  • Generally, if you get a compiler error and you want help to resolve the error, *post the error message*. The error message is there to tell what is wrong. It's much easier for people to help if they know the error message, and the source code line in which it occurs. Without that information you make it much harder for people to help. – sth Jul 07 '15 at 00:04
  • @jdl i have to read character by character because input file is irregular. – bdllhtlgn Jul 07 '15 at 01:21
  • @sth Apologize to you for question that i wrote unskilfully. It is my first question. Sorry. – bdllhtlgn Jul 07 '15 at 01:29
  • @bdllhtlgn... you can load the whole file into an array with fgetc. Then you str functions to search the file as a string to get indexes for your items – jdl Jul 07 '15 at 01:29
  • @jdl please show me how i do it. Then i want to ask is it importat number of characters? some files may be 80000-200000 line. – bdllhtlgn Jul 07 '15 at 01:37

3 Answers3

2

You should probably use strcmp() instead of == to check if two strings are equal.

You also don't seem to be using variable c even though you are checking it against EOF. And are you sure c should be a char *?

Be more descriptive in what kind of error you are getting and write code in smaller steps so it is easier to find your bugs.

sth
  • 222,467
  • 53
  • 283
  • 367
kgwong
  • 121
  • 2
  • 4
1

Well you can use fgets() here to store whole file into an array.

Here as in your code-

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

Instead of this you can use fgets() -

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

You don't have to check for "stop" or EOF here as fgets() itself will return as it encounters EOF.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • Thanks your answer @Ameycu. I don't want to write file from the top on down. I want to just read characters which is between "start" and "stop". Please look at test.txt, start and stop are only characters. They will be chance. – bdllhtlgn Jul 07 '15 at 10:26
  • @bdllhtlgn well then you can read the array and then read the required part and store in another file.That will be more simple as compared to search into file and then to read from your selected part. – ameyCU Jul 07 '15 at 13:08
0

I modified the code using by strcmp() command and removing * than i compiled code with cygwin64.

My Error Code:

$ 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)

Modified Code:

#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;
}
bdllhtlgn
  • 31
  • 7
  • For a follow up question, best open a new question thread. More people will see it that way. But first try to find out where exactly that segmentation fault happens. The best way to do so would be to use a debugger, like `gdb`. - My first guess would be that printing of `swA` is the problem, since the string in `swA` is not null terminated. But you might see a different problem. You are also still not using `==` instead of `strcmp()` to compare strings. – sth Jul 07 '15 at 11:51