Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
jobrod
/
week2 - Jordan brodie
This project
Loading...
Sign in
Toggle navigation
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
Commit
09e80d54
authored
Feb 18, 2025
by
jobrod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Delete Task 1.0 -week 2 Jordan Brodie
parent
df1488b8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
0 additions
and
179 deletions
Task 1.0 -week 2 Jordan Brodie
Task 1.0 -week 2 Jordan Brodie
deleted
100644 → 0
View file @
df1488b8
#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");
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment