-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFILE.C
More file actions
90 lines (76 loc) · 1.5 KB
/
FILE.C
File metadata and controls
90 lines (76 loc) · 1.5 KB
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
/* Write a program to read the text file ,
one line at a time and
print the line read with text reversed.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 150
char stk[150];
int top = -1;
void push(char stk[], char val);
void pop(char stk[]);
int main()
{
FILE *fp;
char line[150];
int i, len;
clrscr();
fp = fopen("sample.txt", "r");
if(fp == NULL)
{
printf("\nERROR-Unable to open file");
}
else
{
while(1)
{
if(fgets(line, 150, fp) == NULL)
break;
len = strlen(line);
printf("\n\nThe contents of the file are: \n");
for(i = 0; i < len; i++)
{
printf("%c", line[i]);
push(stk, line[i]);
}
printf("\n\nThe reverse of the contents is: \n");
for(i = 0; i < len; i++)
{
pop(stk);
}
}
}
fclose(fp);
getch();
return 0;
}
void push(char stk[], char val)
{
if(top == MAX-1)
{
printf("\nSTACK OVERFLOW");
}
else
{
top++;
stk[top] = val;
}
}
void pop(char stk[])
{
if(top == -1)
{
printf("\nSTACK UNDERFLOW");
}
else
{
printf("%c", stk[top]);
top--;
}
}
/* OUTPUT
The contents of the file are:
Deal with your problems before they deal with your happiness.
The reverse of the contents is:
.ssenippah ruoy htiw laed yeht erofeb smelborp ruoy htiw laeD
*/