-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixedSizeRecords.c
More file actions
118 lines (111 loc) · 2.78 KB
/
fixedSizeRecords.c
File metadata and controls
118 lines (111 loc) · 2.78 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
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
#include <stdio.h>
#include <stdlib.h>
#include "funcoesFornecidas.h"
#include <string.h>
char *readLineDynamic(FILE *stream);
void linebreak(FILE *stream);
void writeString(FILE *bin, char* str, int size);
int main(){
int cmd;
scanf("%d ", &cmd);
char* name = readLineDynamic(stdin);
switch(cmd){
case 1: {
FILE* bin = fopen(name, "wb");
int n;
scanf("%d", &n);
linebreak(stdin);
char* str = malloc(sizeof(char) * 81);
for(int i = 0; i < 81; i++) str[i] = '$';
while(n--){
writeString(bin, str, 51);
writeString(bin, str, 51);
writeString(bin, str, 81);
writeString(bin, str, 51);
int age;
scanf("%d", &age);
linebreak(stdin);
fwrite(&age, sizeof(int), 1, bin);
}
free(str);
fclose(bin);
binarioNaTela(name);
free(name);
}
break;
case 2: {
FILE* bin = fopen(name, "rb");
free(name);
if(!bin){
printf("Falha no processamento do arquivo\n");
return 0;
}
char* str = malloc(sizeof(char) * 81);
fread(str, sizeof(char), 51, bin);
while(!feof(bin)){
printf("Firstname: %s\n", str);
fread(str, sizeof(char), 51, bin);
printf("Lastname: %s\n", str);
fread(str, sizeof(char), 81, bin);
printf("Email: %s\n", str);
fread(str, sizeof(char), 51, bin);
printf("Nationality: %s\n", str);
int age;
fread(&age, sizeof(int), 1, bin);
printf("Age: %d\n\n", age);
fread(str, sizeof(char), 51, bin);
}
free(str);
fclose(bin);
}
break;
case 3: {
FILE* bin = fopen(name, "rb");
free(name);
if(!bin){
printf("Falha no processamento do arquivo\n");
return 0;
}
int n;
scanf("%d", &n);
linebreak(stdin);
char* str = malloc(sizeof(char) * 81);
fseek(bin, n * 238, SEEK_SET);
fread(str, sizeof(char), 51, bin);
printf("Firstname: %s\n", str);
fread(str, sizeof(char), 51, bin);
printf("Lastname: %s\n", str);
fread(str, sizeof(char), 81, bin);
printf("Email: %s\n", str);
fread(str, sizeof(char), 51, bin);
printf("Nationality: %s\n", str);
free(str);
int age;
fread(&age, sizeof(int), 1, bin);
fclose(bin);
printf("Age: %d\n\n", age);
}
break;
}
return 0;
}
char *readLineDynamic(FILE *stream){
char *line = NULL;
for(int i = 0; !i || (line[i - 1] != '\0'); i++){
line = realloc(line, (i + 1) * sizeof(char));
char tmp = fgetc(stream);
line[i] = ((tmp == '\n') || feof(stream) || (tmp == '\r')) ? '\0' : tmp;
if(tmp == '\r') fgetc(stream);
}
return line;
}
void linebreak(FILE *stream){
char crlf = fgetc(stream);
while((crlf != '\n') && !feof(stream)) crlf = fgetc(stream);
}
void writeString(FILE *bin, char* str, int size){
readline(str);
fwrite(str, sizeof(char), size, bin);
for(int i = 0; i < strlen(str); i++) str[i] = '$';
str[strlen(str)] = '$';
}