NicholasAdamou

Read a file line by line Example (C)

Dec 8th, 2019
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. /**
  2.  * A C program to demonstrate the reading a file line by line.
  3.  *
  4.  * "config.txt" must exist at the root of the project.
  5.  * contains #,#,#,#,#
  6.  *
  7.  * @author Nicholas Adamou
  8.  * @date 12/7/2019
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. typedef struct TestCase TestCase;
  16.  
  17. struct TestCase {
  18.     int BSIZE;
  19. };
  20.  
  21. char **split(char *str, const char separator);
  22. TestCase *createTestCase(char *data);
  23. TestCase **readFile(char *path, int number_of_test_cases);
  24.  
  25. TestCase *createTestCase(char *data) {
  26.     TestCase *test_case = (TestCase *)malloc(sizeof(TestCase));
  27.     test_case->BSIZE = atoi(&data[0]);
  28.  
  29.     return test_case;
  30. }
  31.  
  32. char **split(char *str, const char separator)
  33. {
  34.     char **result = 0;
  35.     size_t count = 0;
  36.     char *tmp = str;
  37.     char *last_comma = 0;
  38.     char delim[2];
  39.     delim[0] = separator;
  40.     delim[1] = 0;
  41.  
  42.     // Count how many elements will be extracted.
  43.     while (*tmp)
  44.     {
  45.         if (separator == *tmp)
  46.         {
  47.             count++;
  48.             last_comma = tmp;
  49.         }
  50.  
  51.         tmp++;
  52.     }
  53.  
  54.     // Add space for trailing token.
  55.     count += last_comma < (str + strlen(str) - 1);
  56.  
  57.     // Add space for terminating null string so caller
  58.     // knows where the list of returned strings ends.
  59.     count++;
  60.  
  61.     result = malloc(sizeof(char *) * count);
  62.  
  63.     if (result)
  64.     {
  65.         size_t i = 0;
  66.         char *token = strtok(str, delim);
  67.  
  68.         while (token)
  69.         {
  70.             result[i++] = strdup(token);
  71.             token = strtok(NULL, delim);
  72.         }
  73.  
  74.         result[i] = 0;
  75.     }
  76.  
  77.     return result;
  78. }
  79.  
  80. TestCase **readFile(char *path, int number_of_test_cases)
  81. {
  82.     FILE *file = fopen(path, "r");
  83.  
  84.     if (file)
  85.     {
  86.         TestCase **test_cases = (TestCase **)malloc(sizeof(TestCase) * number_of_test_cases);
  87.  
  88.         char *line;
  89.         size_t length;
  90.         size_t read;
  91.         int index = 0;
  92.  
  93.         while ((read = getline(&line, &length, file)) != -1)
  94.         {
  95.             printf("%s", line);
  96.             char **data = split(line, ',');
  97.             test_cases[index++] = createTestCase(line);
  98.         }
  99.  
  100.         fclose(file);
  101.  
  102.         return test_cases;
  103.     }
  104.  
  105.     return NULL;
  106. }
  107.  
  108. int main(int argc, char **argv)
  109. {
  110.     TestCase **data = readFile("./config.txt", 200);
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment