Created
July 25, 2023 22:23
-
-
Save TheGammaSqueeze/430d0abdeba9492ac2fc2e793fd2c2c3 to your computer and use it in GitHub Desktop.
Sample code to generate and read a CSV file for use in an integer mapping table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <errno.h> | |
| #define MAX_TABLE_SIZE 100 | |
| // Define the struct to hold the table data | |
| struct Transformation { | |
| int originalValue; | |
| int transformedValue; | |
| }; | |
| // Function to create and initialize the CSV file | |
| void createCSVFile(struct Transformation table[], int count) { | |
| FILE *csvFile = fopen("table.csv", "w"); | |
| if (csvFile != NULL) { | |
| // Add column headers to the CSV file | |
| fprintf(csvFile, "OriginalValue,TransformedValue\n"); | |
| // Add the data from the table to the CSV file | |
| for (int i = 0; i < count; i++) { | |
| fprintf(csvFile, "%d,%d\n", table[i].originalValue, table[i].transformedValue); | |
| } | |
| // Close the file after writing data | |
| fclose(csvFile); | |
| } else { | |
| printf("Error creating the CSV file. Exiting.\n"); | |
| } | |
| } | |
| int main() { | |
| // Declare an array of structs to hold the table data | |
| struct Transformation table[MAX_TABLE_SIZE]; | |
| // Check if the file exists | |
| FILE *csvFile = fopen("table.csv", "r"); | |
| if (csvFile == NULL) { | |
| printf("CSV file doesn't exist. Creating a new CSV file.\n"); | |
| // Add some sample values (replace with your desired values) | |
| int count = 3; | |
| struct Transformation sampleTable[] = { | |
| {1, 10}, | |
| {2, 20}, | |
| {3, 30} | |
| }; | |
| createCSVFile(sampleTable, count); | |
| printf("New CSV file created with column headers and sample values.\n"); | |
| } else { | |
| fclose(csvFile); | |
| } | |
| // File exists, so close it and reopen in read-write mode (for further operations) | |
| csvFile = fopen("table.csv", "r+"); | |
| if (csvFile == NULL) { | |
| printf("Error opening the CSV file for read-write access. Exiting.\n"); | |
| return 1; | |
| } | |
| // Read data from the CSV file and populate the table | |
| int count = 0; | |
| char line[100]; // Assuming the line won't exceed 100 characters | |
| while (fgets(line, sizeof(line), csvFile) != NULL && count < MAX_TABLE_SIZE) { | |
| // Skip the first line (column headers) | |
| if (strstr(line, "OriginalValue,TransformedValue") != NULL) { | |
| continue; | |
| } | |
| // Use strtok to parse the CSV line and extract the two integers | |
| char *token = strtok(line, ","); | |
| char *endptr; | |
| errno = 0; | |
| // Parse originalValue | |
| int originalValue = strtol(token, &endptr, 10); | |
| if (errno == ERANGE || *endptr != '\0') { | |
| printf("Error: Invalid originalValue integer in the CSV file: %s\n", token); | |
| continue; | |
| } | |
| // Parse transformedValue | |
| token = strtok(NULL, ","); | |
| int transformedValue; | |
| if (sscanf(token, "%d", &transformedValue) != 1) { | |
| printf("Error: Invalid transformedValue integer in the CSV file: %s\n", token); | |
| continue; | |
| } | |
| // Store the valid values in the table | |
| table[count].originalValue = originalValue; | |
| table[count].transformedValue = transformedValue; | |
| count++; | |
| } | |
| // Close the CSV file | |
| fclose(csvFile); | |
| // Now, you have the table imported from the CSV file. | |
| // You can use it to transform specific integer values. | |
| int input_value = 3; // For example, the input value you want to transform | |
| int transformed_value = -1; // Initialize to a default value | |
| // Find the corresponding transformed value from the table | |
| for (int i = 0; i < count; i++) { | |
| if (table[i].originalValue == input_value) { | |
| transformed_value = table[i].transformedValue; | |
| break; // Exit the loop once we find the matching value | |
| } | |
| } | |
| if (transformed_value != -1) { | |
| printf("Input value: %d, Transformed value: %d\n", input_value, transformed_value); | |
| } else { | |
| printf("Input value not found in the table.\n"); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment