Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

jobrod / week2 - Jordan brodie

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • task1week2
  • Task 1 -week 2 Jordan Brodie
Find file
BlameHistoryPermalink
  • jobrod's avatar
    Update Task 1 -week 2 Jordan Brodie · c0ab3042
    jobrod committed 3 months ago
    c0ab3042
Task 1 -week 2 Jordan Brodie 4.46 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

#include <stdio.h>
#include <string.h>

#define CHAR_LEN 20
#define NUM_PEOPLE 10

/// Define a struct to store personal information
typedef struct 
{
    char firstName[CHAR_LEN];
    char lastName[CHAR_LEN];
    char personalCode[CHAR_LEN];
    char matrixNumber[CHAR_LEN];
} person_t;

/// Function prototypes
void fileNameFunction(char*, int);
FILE* openFileFunction(char*);
int readData(FILE*, person_t[], int);
void sortData(person_t[], int);
void displaySortedData(person_t[], int);
void writeOutput(person_t[], int, const char*);
void printFileStatus(int);

int main(void) 
{
    FILE *inputFile;
    char inputName[CHAR_LEN];
    person_t persons[NUM_PEOPLE];

    /// Attempt to open the input file
    inputFile = openFileFunction(inputName);
    if (inputFile == NULL) 
    {
        printFileStatus(0);
        return 1;
    }
    printFileStatus(1);

    /// Read data from the input file
    int records = readData(inputFile, persons, NUM_PEOPLE); 
  
    fclose(inputFile);

    /// Sort the data
    sortData(persons, records);

    /// Display the sorted data
    displaySortedData(persons, records);

    /// Write the sorted data to the output file
    writeOutput(persons, records, "output.txt");

    return 0;
}

/// Prompt the user for the file name
void fileNameFunction(char *inputName, int attempt) 
{
    printf("What is the file name, %d attempts?\n", attempt);
    scanf("%s", inputName);
}

/// Attempt to open the file and return the file pointer
FILE* openFileFunction(char *inputName) 
{
    FILE *inputFile;
    int attempt = 3;
    do 
    {
        fileNameFunction(inputName, attempt);
        inputFile = fopen(inputName, "r");    
        if (inputFile != NULL) 
        {
            return inputFile;
        }
        attempt--;
    } while (attempt != 0);
    return NULL;
}

/// Read data from the input file and store it in the persons array
int readData(FILE *inputFile, person_t persons[], int maxRecords) 
{
    char buffer[512];
    int counter = 0;
    while (fgets(buffer, 512, inputFile) && counter < maxRecords) 
    {
        sscanf(buffer, "%s %s %s %s", persons[counter].firstName, persons[counter].lastName,
               persons[counter].personalCode, persons[counter].matrixNumber);
        counter++;
    }
    return counter;
}

/// Sort the data by personal code
void sortData(person_t persons[], int records) 
{
    for (int i = 0; i < records - 1; i++) 
    {
        for (int j = i + 1; j < records; j++) 
        {
            if (strcmp(persons[i].personalCode, persons[j].personalCode) > 0) 
            {
                person_t temp = persons[i];
                persons[i] = persons[j];
                persons[j] = temp;
            }
        }
    }
}

/// Display the sorted data on the screen
void displaySortedData(person_t persons[], int records) 
{
    printf("\nNumber of records is %d\n", records);
    printf("\nSorted Men:\n");
    for (int i = 0; i < records; i++) 
    {
        if (persons[i].personalCode[0] == '1') 
        {
            printf("%s %s %s %s\n", persons[i].firstName, persons[i].lastName, persons[i].personalCode, persons[i].matrixNumber);
        }
    }

    printf("\nSorted Women:\n");
    for (int i = 0; i < records; i++) 
    {
        if (persons[i].personalCode[0] == '2') 
        {
            printf("%s %s %s %s\n", persons[i].firstName, persons[i].lastName, persons[i].personalCode, persons[i].matrixNumber);
        }
    }
}

/// Write the sorted data to an output file
void writeOutput(person_t persons[], int records, const char *outputFileName) 
{
    FILE *outputFile = fopen(outputFileName, "w");
    if (outputFile == NULL) 
    {
        printf("Error opening output file.\n");
        return;
    }

    fprintf(outputFile, "Men:\n");
    for (int i = 0; i < records; i++) 
    {
        if (persons[i].personalCode[0] == '1') 
        {
            fprintf(outputFile, "%s %s %s %s\n", persons[i].firstName, persons[i].lastName, persons[i].personalCode, persons[i].matrixNumber);
        }
    }

    fprintf(outputFile, "\nWomen:\n");
    for (int i = 0; i < records; i++) 
    {
        if (persons[i].personalCode[0] == '2') 
        {
            fprintf(outputFile, "%s %s %s %s\n", persons[i].firstName, persons[i].lastName, persons[i].personalCode, persons[i].matrixNumber);
        }
    }

    fclose(outputFile);
}

/// Print file status message
void printFileStatus(int status) 
{
    if (status == 1) 
    {
        printf("\nFile exists.\n\n");
    } 
    else 
    {
        printf("File does not exist.\n");
    }
}