Commit 6c52cd2c by jobrod

Update Jordan Brodie 243483MVEB Test1.c

parent 756cb2fa
Showing with 47 additions and 18 deletions
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -17,34 +18,28 @@ void write_file(const char *filename, Record records[], int length); ...@@ -17,34 +18,28 @@ void write_file(const char *filename, Record records[], int length);
void convert_to_hex(int number, char *hex); void convert_to_hex(int number, char *hex);
int is_faulty_record(const char *line); int is_faulty_record(const char *line);
int get_file_length(const char *filename); int get_file_length(const char *filename);
void close_file(FILE *file);
void print_file_length(const char *filename);
void process_records(Record records[], int length);
void print_processed_records(Record records[], int length);
void handle_read_file_error();
int main() int main()
{ {
Record records[MAX_RECORDS]; Record records[MAX_RECORDS];
int length = 0; int length = 0;
int file_length = get_file_length("AN.txt"); print_file_length("AN.txt");
printf("Length of the file: %d lines\n", file_length);
if (read_file("AN.txt", records, &length) != 0) if (read_file("AN.txt", records, &length) != 0)
{ {
printf("Error reading input file.\n"); handle_read_file_error();
return 1; return 1;
} }
for (int i = 0; i < length; i++) process_records(records, length);
{
convert_to_hex(records[i].number, records[i].hex);
}
write_file("TUL.txt", records, length); write_file("TUL.txt", records, length);
print_processed_records(records, length);
printf("Processed Records:\n");
printf("Jnr. I Number I Hex\n");
for (int i = 0; i < length; i++)
{
printf("%-4d I %-6d I %-4s\n", i + 1, records[i].number, records[i].hex);
}
return 0; return 0;
} }
...@@ -78,7 +73,7 @@ int read_file(const char *filename, Record records[], int *length) ...@@ -78,7 +73,7 @@ int read_file(const char *filename, Record records[], int *length)
} }
*length = index; *length = index;
fclose(file); close_file(file);
return 0; return 0;
} }
...@@ -97,7 +92,7 @@ void write_file(const char *filename, Record records[], int length) ...@@ -97,7 +92,7 @@ void write_file(const char *filename, Record records[], int length)
fprintf(file, "%-4d I %-6d I %-4s\n", i + 1, records[i].number, records[i].hex); fprintf(file, "%-4d I %-6d I %-4s\n", i + 1, records[i].number, records[i].hex);
} }
fclose(file); close_file(file);
} }
void convert_to_hex(int number, char *hex) void convert_to_hex(int number, char *hex)
...@@ -131,6 +126,40 @@ int get_file_length(const char *filename) ...@@ -131,6 +126,40 @@ int get_file_length(const char *filename)
count++; count++;
} }
fclose(file); close_file(file);
return count; return count;
} }
void close_file(FILE *file)
{
fclose(file);
}
void print_file_length(const char *filename)
{
int file_length = get_file_length(filename);
printf("Length of the file: %d lines\n", file_length);
}
void process_records(Record records[], int length)
{
for (int i = 0; i < length; i++)
{
convert_to_hex(records[i].number, records[i].hex);
}
}
void print_processed_records(Record records[], int length)
{
printf("Processed Records:\n");
printf("Jnr. I Number I Hex\n");
for (int i = 0; i < length; i++)
{
printf("%-4d I %-6d I %-4s\n", i + 1, records[i].number, records[i].hex);
}
}
void handle_read_file_error()
{
printf("Error reading input file.\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