Deletes text in brackets from a txt file

Hello!

I need a little help with my code. Program should open a txt file, read it and make a new one with the same text but eliminate text in brackets.

For example>>
Opened file>>> abc (gta bty) nda (cde
fgy adc) dmy nb
Created file>>> abc nda dmy nb

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

This is what I have done, but it is not enough. Because it works only with given example, if I add some more words in brackets it will give me wrong output. Any ideas???
int main(void){
char tmp[50], tmp1[50];
char * pch;
char ni[15],no[15];
FILE *fi, *fo;
printf("File to be modified is: ");
scanf("%s",ni);
printf("Result file is: ");
scanf("%s",no);
fi=fopen(ni,"r");
fo=fopen(no,"w");
while (!feof(fi)){
fscanf(fi, "%s", &tmp);
if(pch=strchr(tmp,'(')){

fscanf(fi, "%s", &tmp);
if(pch=strchr(tmp,')')){
}else{
fscanf(fi, "%s", &tmp);
if(pch=strchr(tmp,')')){
}
else{
fscanf(fi, "%s", &tmp);
}
}

}else{
printf("%s ", tmp);
}
}
fclose(fi);
fclose(fo);
getchar();
getchar();
}


closed account (S6k9GNh0)
Put your code in [CODE] bbcode.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <string.h>

/*This is what I have done, but it is not enough. Because it works only with given example, if I
 add some more words in brackets it will give me wrong output. Any ideas???
*/
int main(void)
{
     char tmp[50], tmp1[50];
     char * pch;
     char ni[15],no[15];
     FILE *fi, *fo;

     printf("File to be modified is: ");

     scanf("%s",ni);

     printf("Result file is: ");

     scanf("%s",no);

     fi=fopen(ni,"r");

     fo=fopen(no,"w");

     while (!feof(fi))
     {
          fscanf(fi, "%s", &tmp);
          if(pch=strchr(tmp,'('))
          {

               fscanf(fi, "%s", &tmp);
               if(pch=strchr(tmp,')'))
               {
               }
               else
               {
                    fscanf(fi, "%s", &tmp);
                    if(pch=strchr(tmp,')'))
                    {
                    }             
                    else
                    {
                        fscanf(fi, "%s", &tmp);
                    }
               }

           }
           else{
            printf("%s ", tmp);
            }
      }
      fclose(fi);
      fclose(fo);
      getchar();
      getchar();
}
Last edited on
thnx oghmaosiris . Im new to this forum...
Topic archived. No new replies allowed.