-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFILEDOC.C
117 lines (91 loc) · 1.69 KB
/
FILEDOC.C
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Assignment Question no. 2 */
#include<stdio.h>
#include<string.h>
#include<alloc.h>
struct node
{
char a[10];
int lineno[10];
int index;
struct node *link;
};
void store(struct node *,char [20],int);
void display(struct node *);
void main(void)
{
FILE *fp;
char c[20];
char b;
int i,j,l=0;
struct node *first,*x;
clrscr();
fp=fopen("abc.txt","r+");
j=1;
fscanf(fp,"%s",c);
x=(struct node *)malloc(sizeof(struct node));
strcpy(x->a,c);
x->lineno[0]=j;
x->index=0;
x->link=NULL;
first=x;
fseek(fp,1,SEEK_CUR);
while(!feof(fp))
{
fscanf(fp,"%s",c);
b=fgetc(fp);
if ((b=='\n')||(b==' '))
{
store(first,c,j);
if (b==' ') l++;
if (b=='\n') j++;
}
}
fclose(fp);
display(first);
}
/* creation of node and storing the value in the link list */
void store(struct node *first,char c[20],int j)
{
struct node *n,*ptr;
int k;
ptr=first;
while((ptr->link!=NULL)&&(strcmpi(c,ptr->a)!=0))
ptr=ptr->link;
if ((ptr->link==NULL)&&(strcmpi(c,ptr->a)!=0))
{
n=(struct node *)malloc(sizeof(struct node));
n->link=NULL;
k=0;
strcpy(n->a,c);
n->lineno[k]=j;
n->index=k;
ptr->link=n;
strcpy(c," ");
}
if ((strcmpi(c,ptr->a)==0))
{
(ptr->index)++;
ptr->lineno[ptr->index]=j;
}
return;
}
/* display the actual list */
void display(struct node *first)
{
struct node *ptr;
int i;
ptr=first;
while (ptr!=NULL)
{
printf("\n");
for(i=0;i<strlen(ptr->a);i++)
printf("%c",ptr->a[i]);
printf(":");
for(i=0;i<=ptr->index;i++)
{
printf("\n line no.%d ",ptr->lineno[i]);
}
printf("\n");
ptr=ptr->link;
}
}