file program problem

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

#include<stdio.h>

int main()
{

	char ch, file_name[25];
	char wordChoice[2]="->";
	int count=0,z=0,i=0;
   	FILE *fp;
	char *a[100];

 
   
 
   fp = fopen("mergesort1.txt","r");
 
   if( fp == NULL )
   {
      perror("Error while opening the file.\n");
      
   }
 
   printf("The contents of %s file are :\n", file_name);
 
   char buffer[2024]; //Sorry for this.
        while(fgets(buffer, 2024, fp) != NULL)
        {

		if(z==1)
		{
		printf("%s",buffer);
		
		z=0;
		}
            if(strstr(buffer, wordChoice))
            {count++;
		z=z+1;
		
                
            }
	

        }
 
   fclose(fp);


	printf("count:%d \n",count);
	
	

}



This is the code which searches for a string ie(->)in a file which is attached below and on finding it prints the next word after the symbol.
In this code how is it possible to print the previous word before the symbol and save it in an array.


eg the file is this:



digraph
OPcontrolflow
graph
centertrue
epsilon000001
packtrue
rankdirLR
ratiocompress
sep5
size583827
splinestrue
node
fillcolorfff7f7
shapedoublecircle
stylefilled
B02
fillcolorffe6e6
shapecircle
B01
->
B02
B03
shapecircle
B02
->
B03
B04
shapecircle
B03
->
B04
B05
shapecircle
B04
->
B05
B06
fillcolorffe6e6
shapecircle
B05
->
B06
B07
shapecircle
B06
->
B07
B08
fillcolorffe6e6
shapecircle
B07
->
B08
B09
shapecircle
B08
->
B09
B10
fillcolorffe6e6
shapecircle
B09
->
B10
B11
shapecircle
B10
->
B11
B12
fillcolorffb3b3
shapecircle
B11
->
B12
B13
fillcolorffe6e6
shapecircle
B12
->
B13
label3
B15
fillcolorffb3b3
shapecircle
B12
->
B15
label2
B14
fillcolorffcccc
shapecircle
B13
->
B14
label3
B14
->
B15
label3
B16
fillcolorffdddd
shapecircle
B15
->
B16
B17
fillcolorff0000
shapecircle
B16
->
B17
B18
fillcolorff5555
shapecircle
B17
->
B18
label5
B21
fillcolorffeeee
shapecircle
B17
->
B21
label2
B25
fillcolorffbbbb
shapecircle
B17
->
B25
label2
B19
fillcolorffaaaa
shapecircle
B18
->
B19
label2
B22
fillcolorffe6e6
shapecircle
B18
->
B22
label3
B20
fillcolorffeeee
shapecircle
B19
->
B20
label2
B20
->
B21
label2
B23
fillcolorff9999
shapecircle
B22
->
B23
label3
B24
fillcolorffe6e6
shapecircle
B23
->
B24
label3
B24
->
B25
B26
fillcolorffaaaa
shapecircle
B24
->
B26
label2
B27
fillcolorffeeee
shapecircle
B26
->
B27
label2
B28
fillcolorffeeee
shapecircle
B27
->
B28
label2
B29
fillcolorffeeee
shapecircle
B28
->
B29
label2
B30
fillcolorffeeee
shapecircle
B29
->
B30
label2
B31
fillcolorffeeee
shapecircle
B30
->
B31
label2
B32
fillcolorffeeee
shapecircle
B31
->
B32
label2
B33
fillcolorffeeee
shapecircle
B32
->
B33
label2
B34
fillcolorffbbbb
shapecircle
B33
->
B34
label2
B35
fillcolorffdddd
shapecircle
B34
->
B35
label2
B36
fillcolorff9999
shapecircle
B35
->
B36
label2
B37
fillcolorffeeee
shapecircle
B36
->
B37
B55
shapecircle
B36
->
B55
B38
shapecircle
B37
->
B38
B39
fillcolorffe6e6
shapecircle
B38
->
B39
B40
fillcolorffeeee
shapecircle
B39
->
B40
B41
fillcolorffdddd
shapecircle
B40
->
B41
B42
fillcolorffeeee
shapecircle
B41
->
B42
B58
fillcolorffeeee
shapecircle
B41
->
B58
B43
fillcolorffeeee
shapecircle
B42
->
B43
B44
shapecircle
B43
->
B44
B45
fillcolorffe6e6
shapecircle
B44
->
B45
B46
fillcolorffcccc
shapecircle
B45
->
B46
B47
shapecircle
B46
->
B47
B48
fillcolorffdddd
shapecircle
B47
->
B48
B49
fillcolorffdddd
shapecircle
B48
->
B49
label2
B50
fillcolorffeeee
shapecircle
B49
->
B50
label2
B51
fillcolorffaaaa
shapecircle
B50
->
B51
label2
B52
fillcolorff2b2b
shapecircle
B51
->
B52
label5
B51
->
B52
label3
B53
fillcolorffeeee
shapecircle
B52
->
B53
label2
B54
fillcolorffeeee
shapecircle
B53
->
B54
label2
B54
->
B25
B63
fillcolorffe6e6
shapecircle
B54
->
B63
B56
fillcolorffeeee
shapecircle
B55
->
B56
B57
fillcolorffe6e6
shapecircle
B56
->
B57
B57
->
B41
B59
fillcolorffe6e6
shapecircle
B58
->
B59
B60
fillcolorff9999
shapecircle
B59
->
B60
B61
fillcolorffeeee
shapecircle
B60
->
B61
label2
B60
->
B61
B62
shapecircle
B61
->
B62
B62
->
B48
B64
shapecircle
B63
->
B64
B65
fillcolorffe6e6
shapecircle
B64
->
B65
B66
fillcolorffeeee
shapecircle
B65
->
B66
B67
fillcolorff9999
shapecircle
B66
->
B67
B68
fillcolorffcccc
shapecircle
B67
->
B68
label3
B70
fillcolorffcccc
shapecircle
B67
->
B70
label2
B69
fillcolorffe6e6
shapecircle
B68
->
B69
label3
B69
->
B70
label3
B71
shapecircle
B70
->
B71
B72
fillcolorffeeee
shapecircle
B71
->
B72
B73
shapecircle
B72
->
B73
B73
->
B74


In this code how is it possible to print the previous word before the symbol and save it in an array.

You can save the current value in a string, overwriting it at the end of each read.

When you read your print token, print that previous value.

Ypu already have an array:
 
char* a[100];
You can save it there. You'll need a number to hold the next free index.

As the array is an array of char*, you can fill each item with a copy to the string you want to save there with strdup.

For example:
1
2
3
4
5
6
char* array[100];
int idx = 0;

//...

array[idx] = strdup(buffer);
1
2
3
4
5
6
7
8
9

if(z==1)
		{
		printf("%s",buffer);
		a[i]=strdup(buffer);
		i++;
		z=0;
		}



i added this code and then tried to print the array after closing the file.

1
2
3
4
5
6

		for(i=0;i<100;i++)
		{
		printf("%s",a[i]);
		}



It gives segmentation dump error
Topic archived. No new replies allowed.