I am trying to understand what I am doing wrong. The input from keyboard is within the char array limits... even if the input is 8 chars long, it throws an error. it works fine as long as the char is 6 chars long.
This is my code (I can swear it worked until 1h ago)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "functions.h"
#define SLEN 81
#define FILE_NAME "../database.txt"
void add_members(char *name);
int main(void)
{
char name[SLEN], surname[SLEN], address[SLEN];
unsigned int dob, start_date, end_date;
add_members(&name[SLEN]);
return 0;
}
void add_members(char *name)
{
FILE *file;
if ((file = fopen(FILE_NAME, "a")) == NULL)
{
printf("cant open file %s\n", FILE_NAME);
exit(EXIT_FAILURE);
}
fputs("Insert FIRST NAME: ", stdout);
fgets(name, SLEN, stdin); // appears the problem to be here
check_string(name); // this is defined in another file
fputs(name, file);
printf("Name added to database: %s\n", name);
if (fclose(file) != 0)
printf("error in closing file %s\n", FILE_NAME);
printf("File: %s closed\n", FILE_NAME);
}
add_members(&name[SLEN]);
That is not correct because &name[SLEN]
is the byte after the end of the array. What is needed is the start of the array.
add_members(name);
or add_members(&name[0]);