Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- //Name: Isaac Di Francesco
- //ID: 260868693
- int main(int argc, char *argv[]) {
- //check if at least one argument was passed for CMD
- if (argc == 1) {
- puts("You did not provide any arguments. Please enter: ./database CMD OPT1 OPT2 OPT3 OPT4");
- return -1;
- }
- //input variables
- char *CMD = argv[1];
- char *OPT1 = argv[2];
- char *OPT2 = argv[3];
- char *OPT3 = argv[4];
- char *OPT4 = argv[5];
- int ID, AGE;
- float GPA;
- int charCount = 0, lineCount = 1, fieldCount = 0; //counters for loop
- //check CMD input
- if (strcmp(CMD, "SHOW") == 0) {
- FILE *dataFile;
- dataFile = fopen("database.csv", "r");
- //variables for each line
- char *line = NULL;
- size_t length = 0;
- char field1[100], field2[100], field3[100], field4[100];
- //iterate through CSV file and get each line
- while (getline(&line, &length, dataFile) != -1) {
- //reset the field arrays
- memset(field1, 0, sizeof field1);
- memset(field2, 0, sizeof field2);
- memset(field3, 0, sizeof field3);
- memset(field4, 0, sizeof field4);
- //update parameters
- fieldCount = 0;
- charCount = 0;
- //get first field
- while (line[charCount] != ',') {
- //check for space
- if (line[charCount] != ' ') {
- field1[fieldCount] = line[charCount];
- fieldCount++;
- }
- charCount++;
- }
- // update parameters
- charCount++;
- fieldCount = 0;
- // get second field
- while (line[charCount] != ',') {
- //check for space
- if (line[charCount] != ' ') {
- field2[fieldCount] = line[charCount];
- fieldCount++;
- }
- charCount++;
- }
- // update parameters
- charCount++;
- fieldCount = 0;
- // get third field
- while (line[charCount] != ',') {
- //check for space
- if (line[charCount] != ' ') {
- field3[fieldCount] = line[charCount];
- fieldCount++;
- }
- charCount++;
- }
- // update parameters
- charCount++;
- fieldCount = 0;
- // get fourth field
- while (line[charCount] != '\n') {
- //check for space
- if (line[charCount] != ' ') {
- field4[fieldCount] = line[charCount];
- fieldCount++;
- }
- charCount++;
- }
- //format fields to proper data type
- sscanf(field1, "%d", &ID);
- sscanf(field3, "%d", &AGE);
- sscanf(field4, "%f", &GPA);
- //display record
- printf("Record %d: ID=%-3d NAME=%-5s AGE=%-3d GPA=%-3.1f \n", lineCount, ID, field2, AGE, GPA);
- lineCount++;
- }
- //free memory location for line & close file
- free(line);
- fclose(dataFile);
- } else if (strcmp(CMD, "DELETE") == 0) {
- //check if OPT1 has been passed
- if (argc <= 2) {
- printf("Name of record to delete is missing\n");
- return -1;
- }
- // open database file
- FILE *dataFile;
- FILE *tempFile;
- dataFile = fopen("database.csv", "r");
- tempFile = fopen("database.tmp", "w");
- //variables for each line
- char *line = NULL;
- size_t length = 0;
- char fieldCheck[100];
- int i = 0, j = 0;
- int notFound = 1;
- //iterate through database file
- while (getline(&line, &length, dataFile) != -1) {
- i = 0;
- j = 0;
- while (line[i] != ',') {
- //check for space
- if (line[i] != ' ') {
- fieldCheck[j] = line[i];
- j++;
- }
- i++;
- }
- //end string for further iterations of loop
- fieldCheck[j] = '\0';
- // check if ID field from current line matches OPT1 and ID hasn't already been found
- if (strcmp(fieldCheck, OPT1) == 0 && notFound) {
- // exclude line from copy
- notFound = 0;
- } else {
- // copy line to temp file
- fputs(line, tempFile);
- }
- }
- if (notFound) {
- printf("Sorry, that user was not found. Nothing was deleted.\n");
- system("rm database.tmp");
- } else {
- system("mv database.tmp database.csv");
- }
- //free memory location of line & close files
- free(line);
- fclose(dataFile);
- fclose(tempFile);
- } else if (strcmp(CMD, "ADD") == 0) {
- // check if OPT variables have been passed
- if (argc < 6) {
- printf("Missing ID, Name, AGE, and GPA arguments\n");
- return -1;
- }
- FILE *dataFile;
- dataFile = fopen("database.csv", "a");
- //add line to datafile
- fputs(OPT1, dataFile);
- fputs(",", dataFile);
- fputs(OPT2, dataFile);
- fputs(",", dataFile);
- fputs(OPT3, dataFile);
- fputs(",", dataFile);
- fputs(OPT4, dataFile);
- fputs("\n", dataFile);
- //close datafile
- fclose(dataFile);
- } else {
- printf("The command you requested is invalid. Please select from one of these: SHOW, DELETE, ADD\n");\
- return -1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement