brmbar-github/dataio.c
Cestmir Houska 948d2630a4 Added dataio.h/c
Signed-off-by: Cestmir Houska <czestmyr@gmail.com>
2011-04-22 04:25:15 +02:00

39 lines
898 B
C

#include <stdio.h>
#include <string.h>
#include "dataio.h"
int modify_credit(name, price)
const char* name;
int price;
{
char filename[13];
int i;
int credit;
FILE* person_data;
strncpy(filename, name, 8);
i = strlen(name);
if (i > 8) i = 8;
strcpy(&filename[i], ".txt");
person_data = fopen(filename, "r");
if (person_data == NULL) {
printf("Warning: Filename %s does not exist! Creating...\n", filename);
credit = 0;
} else {
fscanf(person_data, "%i", &credit);
fclose(person_data);
}
credit += price;
person_data = fopen(filename, "w");
if (person_data == NULL) {
printf("ERROR: Filename %s could not be created or overwritten!\nCheck system integrity!\n", filename);
} else {
fprintf(person_data, "%i", credit);
fclose(person_data);
}
return credit;
}