You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_EXTENSION ".csv"
int main(int argc, char const *argv[])
{
if (argc <= 1)
{
fprintf(stderr, "Failed to provide a csv file\n");
exit(1);
}
size_t file_len = strlen(argv[1]);
/*char pointer of*/
char *FILE_NAME = malloc(file_len + 1);
if (FILE_NAME == NULL)
{
fprintf(stderr, "Failed to allocate memory for file name!");
exit(1);
}
/* Copy file name from executable's first arguments.*/
strcpy(FILE_NAME, argv[1]);
/*Returns a pointer to the location the substring if found*/
if (strstr(FILE_NAME, FILE_EXTENSION) == NULL)
{
fprintf(stderr, "What the fuck are you doing? That %s isn't a csv file!\n", FILE_NAME);
free(FILE_NAME);
FILE_NAME = 0;
exit(1);
}
char name[128], phone_number[13];
// puts(FILE_NAME);
FILE *_target_file = fopen(FILE_NAME, "a");
if (_target_file == NULL)
{
exit(1);
}
puts("Name: ");
scanf("%s", name);
puts("Number: ");
scanf("%s", phone_number);
printf("Appending %s and %s to %s", name, phone_number, FILE_NAME);
fprintf(_target_file, "%s,%s\n", name, phone_number);
fclose(_target_file);
/*clean up*/
free(FILE_NAME);
FILE_NAME = 0;
return 0;
}