/
add.c
48 lines (39 loc) · 1.09 KB
/
add.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
/*
* add.c:
* This function add an acronym to the database,
* it receives the string with the acronym and
* the path to the database.
* It returns 0 on success and a non-zero on failure!
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h"
#include "utilities.h"
int add (char* arg, char* path){
// turn accronym to upper case letters.
const char* upper_arg = to_upper(arg);
// file to acronym file
const char* filePath = strcat(path, arg);
// create the acronym file
FILE* fp = fopen(filePath, "w+x");
if (!fp){ // check if file exists
perror(upper_arg);
exit(EXIT_FAILURE);
} else {
size_t size = SIZE;
char* buffer = (char*) malloc(sizeof(char) * size);
// User prompt for acronym definition.
printf("\nInsert definition for %s : \n> ", upper_arg);
int bytes_read = getline(&buffer, &size, stdin);
if (bytes_read == -1){ // User input error checking.
perror("ERROR!");
exit(EXIT_FAILURE);
}
// write to acronynm file
fprintf(fp, "\n\n\t%s :\n \t\t%s\n\n\n", upper_arg, buffer);
free(buffer);
}
fclose(fp);
return 0;
}