Repository navigation
Expand file tree
/
Copy pathlogout_code.c
More file actions
68 lines (54 loc) · 1.43 KB
/
Copy pathlogout_code.c
File metadata and controls
68 lines (54 loc) · 1.43 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
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#define BUFFERSIZE 4096
int logout(char*);
int main(){
char* username = "testtest123";
logout(username);
return 0;
}
int logout(char* ID){
int fd, lseek_check, read_check, write_check;
int found = 0;
char buf[BUFSIZ];
size_t len;
FILE *file = fopen("login.txt", "r+");
if (file == NULL) {
perror("Failed to open the file");
exit(1);
}
FILE *tempFile = fopen("temp.txt", "w");
if (tempFile == NULL) {
perror("Failed to create temporary file");
fclose(file);
return -1;
}
char line[1000];
while (fgets(line, sizeof(line), file) != NULL) {
if (strncmp(line, ID, strlen(ID)) != 0) {
fputs(line, tempFile);
} else { // ID found, set the flag
found = 1;
}
}
// Close both files
fclose(file);
fclose(tempFile);
if (remove("login.txt") != 0) { // Remove the original login.txt file
perror("Failed to remove the original file");
return -1;
}
if (rename("temp.txt", "login.txt") != 0) { // Rename the temporary file to login.txt
perror("Failed to rename the temporary file");
return -1;
}
if (found) {
printf("Logged out successfully\n");
} else {
printf("ID not found\n");
}
return 0;
}