-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcase1.c
194 lines (140 loc) · 3.44 KB
/
testcase1.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include <string.h>
#include<conio.h>
#include<stdlib.h>
#define BUFFER_SIZE 1000
int indexOf(FILE *fptr, const char *word, int *line, int *col);
void signup()
{
FILE *ptr;
// integer variable
long long int Phone_No; // This for large no.
// character variable
char name[40];
char veh[10];
char so[25];
char us[15];
char password[10];
// open the file in write mode
ptr = fopen("customer.txt", "a");
if (ptr != NULL) {
printf("File created successfully!\n");
}
else {
printf("Failed to create the file.\n");
}
// get student detail
printf("Welcome! Please Enter your information\n");
printf("Enter your name: \n");
scanf("%s",&name);
printf("Enter Phone no: \n");
scanf("%lld",&Phone_No);
printf("Enter Vehcile no: \n");
scanf("%s",&veh);
printf("Enter source: \n");
scanf("%s",&so);
printf("\n --------------------------- ");
printf("\n ENTER USERNAME: \n");
scanf("%s",&us);
printf("\n ENTER PASSWORD: \n");
scanf("%s",&password);
// write data in file
fprintf(ptr, "%s %lld", name, Phone_No);
fprintf(ptr, "%s %s", veh, so);
fprintf(ptr, "%s %s", us, password);
fprintf(ptr, "\n");
// close connection
fclose(ptr);
}
//=====================================================================//=====================================================================
void login()
{
int i;
char name[20], password[20], ch;
printf("Enter your username");
scanf("%s", &name);
printf("password:\n");
int p=0;
do{
password[p]=getch();
if(password[p]!='\r'){
printf("*");
}
p++;
}while(password[p-1]!='\r');
password[p-1]='\0';
printf("Username : %s",name);
printf("\n Password :",password);
getch();
FILE *fptr;
char path[100];
char word[50];
int line, col;
/* Input file path */
printf("Enter file path: ");
scanf("%s", path);
/* Input word to search in file */
printf(" to search in file: ",password);
/* Try to open file */
fptr = fopen(path, "r");
/* Exit if file not opened successfully */
if (fptr == NULL)
{
printf("Unable to open file.\n");
printf("Please check you have read/write previleges.\n");
exit(EXIT_FAILURE);
}
// Find index of word in fptr
indexOf(fptr, password, &line, &col);
if (line != -1)
printf("'%s' found at line: %d, col: %d\n", password, line + 1, col + 1);
else
printf("'%s' does not exists.", password);
// Close file
fclose(fptr);
}
int indexOf(FILE *fptr, const char *password, int *line, int *col)
{
char str[BUFFER_SIZE];
char *pos;
*line = -1;
*col = -1;
while ((fgets(str, BUFFER_SIZE, fptr)) != NULL)
{
*line += 1;
// Find first occurrence of word in str
pos = strstr(str, password);
if (pos != NULL)
{
// First index of word in str is
// Memory address of pos - memory
// address of str.
*col = (pos - str);
break;
}
}
// If word is not found then set line to -1
if (*col == -1)
*line = -1;
return *col;
}
int main()
{
int ch;
printf("Hello, Would you like to log in or register\n[1] Login\n[2] Register\n[3] Exit");
scanf("%d",&ch);
switch (ch)
{
case 1:
login();
break;
case 2:
signup();
break;
case 3:
default:
printf("exit");
break;
}
return 0;
}