Commit 09e80d54 by jobrod

Delete Task 1.0 -week 2 Jordan Brodie

parent df1488b8
Showing with 0 additions and 179 deletions
#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");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment