Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define Max_Line_Length 100 // Maximum length of each line in the input file
- #define Max_Text_Records 10 // Maximum number of text records that can be stored
- // Structure to store details of each text record
- typedef struct TextRecords {
- int address; // Address field of the text record
- int length; // Length of the text record
- char restOfLine[Max_Line_Length]; // Remaining part of the line after address and length
- } TextRecords;
- TextRecords textRecords[Max_Text_Records]; // Array to hold text records
- int textRecordCount = 0; // Counter for the number of text records stored
- int main() {
- char line[Max_Line_Length], restOfLine[Max_Line_Length];
- int address, text_length;
- FILE *f1, *f2;
- // Open input and output files
- f1 = fopen("input.txt", "r");
- f2 = fopen("output.txt", "w");
- // Check if files were opened successfully
- if (f1 == NULL || f2 == NULL) {
- printf("Error! File not found.");
- exit(0);
- }
- // Read each line from the input file
- while (fgets(line, sizeof(line), f1)) {
- // Check if the line starts with 'H' (Header record)
- if (line[0] == 'H') {
- int starting_address, length;
- char name[7];
- // Parse the header record to extract name, starting address, and length
- sscanf(line, "H^%4s^%X^%d", name, &starting_address, &length);
- printf("Starting address is %06X\n", starting_address);
- }
- // Check if the line starts with 'T' (Text record)
- else if (line[0] == 'T') {
- // Parse the text record to extract address, length, and remaining data
- sscanf(line, "T^%X^%X^%[^\n]", &address, &text_length, restOfLine);
- // Store parsed values into the textRecords array
- snprintf(textRecords[textRecordCount].restOfLine, Max_Line_Length, "%s", restOfLine);
- textRecords[textRecordCount].address = address;
- textRecords[textRecordCount].length = text_length;
- textRecordCount++; // Increment the text record count
- }
- // Check if the line starts with 'E' (End record)
- else if (line[0] == 'E') {
- break; // End the loop if 'E' record is found
- }
- }
- // Process each stored text record and write to the output file
- for (int i = 0; i < textRecordCount; ++i) {
- // Tokenize the restOfLine string based on the '^' delimiter
- char *token = strtok(textRecords[i].restOfLine, "^");
- while (token != NULL) {
- int len = strlen(token);
- // Write pairs of characters to output file in specific format
- while (len > 0) {
- fprintf(f2, "%06X %c%c\n", textRecords[i].address++, token[0], token[1]);
- len -= 2; // Move to the next two characters
- token += 2; // Advance the pointer
- }
- token = strtok(NULL, "^"); // Get the next token
- }
- }
- printf("\n--------Output written to output.txt--------\n");
- // Close input and output files
- fclose(f1);
- fclose(f2);
- return 0;
- }
- /*
- input.txt
- -----------------------------------------------------
- H^COPY^001000^00001F
- T^001000^12^123456^011020^051022^231025^678369^000002
- T^001021^12^123456^011020^051022^231025^678369^000002
- E^001000
- -----------------------------------------------------
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement