Fixed non-interactive mode

This commit is contained in:
Jack-Benny Persson 2017-07-10 23:04:53 +02:00
parent 705da77a3a
commit 7d8132d1f8

View File

@ -78,7 +78,7 @@ int main(int argc, char* argv[])
/* Check if file exists, if it dosen't, create it */ /* Check if file exists, if it dosen't, create it */
if ( access(filename, R_OK|W_OK) != 0 ) if ( access(filename, R_OK|W_OK) != 0 )
{ {
printf("Could not open %s\n", filename); fprintf(stderr, "Could not open %s\n", filename);
printf("Create the file and start adding records? (y/n): "); printf("Create the file and start adding records? (y/n): ");
create = getchar(); create = getchar();
if ( create == 'y' ) if ( create == 'y' )
@ -129,7 +129,8 @@ int main(int argc, char* argv[])
else if ( choice == 'n' ) else if ( choice == 'n' )
{ {
data = calloc(1, sizeof(struct myData)); data = calloc(1, sizeof(struct myData));
new(data, numRec); if ( new(data, numRec) == 1 )
return 1;
} }
else else
{ {
@ -206,6 +207,8 @@ void modify(struct myData *datap, int numRec, char *name, char *num)
for (int i = 0; i<numRec; i++) for (int i = 0; i<numRec; i++)
{ {
if ( strcmp(searchword, datap[i].name) == 0 ) if ( strcmp(searchword, datap[i].name) == 0 )
{
if (interactive == 1)
{ {
printHeader(); printHeader();
printf("%-30s\t", datap[i].name); printf("%-30s\t", datap[i].name);
@ -213,12 +216,11 @@ void modify(struct myData *datap, int numRec, char *name, char *num)
printf("%.2f\t", datap[i].price); printf("%.2f\t", datap[i].price);
printf("\n\n"); printf("\n\n");
if (interactive == 1)
{
printf("What do you like to modify? (name, quantity, price): "); printf("What do you like to modify? (name, quantity, price): ");
fgets(what, 10, stdin); fgets(what, 10, stdin);
what[strcspn(what, "\n")] = '\0'; what[strcspn(what, "\n")] = '\0';
} }
if ( strcmp(what, "name") == 0 ) if ( strcmp(what, "name") == 0 )
{ {
printf("Name: "); scanf("%29s", datap[i].name); printf("Name: "); scanf("%29s", datap[i].name);
@ -227,16 +229,18 @@ void modify(struct myData *datap, int numRec, char *name, char *num)
{ {
if (interactive == 1) if (interactive == 1)
{ {
printf("Quantity (absolute value or +/-NUMBER: "); printf("Quantity (absolute value or (a)dd/(s)ubtractNUMBER): ");
fgets(quant, 20, stdin); fgets(quant, 20, stdin);
quant[strcspn(quant, "\n")] = '\0'; quant[strcspn(quant, "\n")] = '\0';
} }
/* Process the first character */ /* Process the first character */
if (quant[0] == '+') if (quant[0] == 'a')
datap[i].quantity = datap[i].quantity + atoi(quant); {
else if (quant[0] == '-') quant[0] = ' '; /* We need to remove the character first */
datap[i].quantity = datap[i].quantity + atoi(quant);
}
else if (quant[0] == 's')
{ {
/* We need to replace the - with a space before atoi */
quant[0] = ' '; quant[0] = ' ';
datap[i].quantity = datap[i].quantity - atoi(quant); datap[i].quantity = datap[i].quantity - atoi(quant);
} }
@ -249,6 +253,12 @@ void modify(struct myData *datap, int numRec, char *name, char *num)
printf("Price: "); scanf("%f", &datap[i].price); printf("Price: "); scanf("%f", &datap[i].price);
} }
} }
else
{
fprintf(stderr, "Could not find %s in database\n", searchword);
exit(1);
}
} }
FILE *newfp = fopen(filename, "wb"); FILE *newfp = fopen(filename, "wb");
fwrite(datap, sizeof(struct myData), numRec, newfp); fwrite(datap, sizeof(struct myData), numRec, newfp);
@ -259,10 +269,13 @@ void delete(struct myData *datap, int numRec, char *name)
{ {
char searchword[NAMEMAXLENGTH]; char searchword[NAMEMAXLENGTH];
int answer; int answer;
int interactive = 1;
if (name != NULL) if (name != NULL)
{ {
strncpy(searchword, name, NAMEMAXLENGTH-1); strncpy(searchword, name, NAMEMAXLENGTH-1);
interactive = 0;
answer = 'y';
} }
else else
{ {
@ -273,6 +286,8 @@ void delete(struct myData *datap, int numRec, char *name)
for (int i = 0; i<numRec; i++) for (int i = 0; i<numRec; i++)
{ {
if ( strcmp(searchword, datap[i].name) == 0 ) if ( strcmp(searchword, datap[i].name) == 0 )
{
if (interactive == 1)
{ {
printHeader(); printHeader();
printf("%-30s\t", datap[i].name); printf("%-30s\t", datap[i].name);
@ -282,6 +297,7 @@ void delete(struct myData *datap, int numRec, char *name)
printf("Delete the record listed above? (y/n): "); printf("Delete the record listed above? (y/n): ");
answer = getchar(); answer = getchar();
}
if ( answer == 'y' ) if ( answer == 'y' )
{ {
FILE *newfp = fopen(filename, "wb"); FILE *newfp = fopen(filename, "wb");
@ -296,6 +312,11 @@ void delete(struct myData *datap, int numRec, char *name)
fclose(newfp); fclose(newfp);
} }
} }
else
{
fprintf(stderr, "Could not find %s in database\n", searchword);
exit(1);
}
} }
} }
@ -328,7 +349,7 @@ int new(struct myData *datap, int numRec)
bytes = fwrite(datap, sizeof(struct myData), 1, fp); bytes = fwrite(datap, sizeof(struct myData), 1, fp);
if (bytes != 1) if (bytes != 1)
{ {
printf("Could not write to the file!\n"); fprintf(stderr, "Could not write to the file!\n");
return 1; return 1;
} }
} }