SHARE
TWEET

CCC

a guest Oct 21st, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This will only have effect on Windows with MSVC
  2. #ifdef _MSC_VER
  3.     #define _CRT_SECURE_NO_WARNINGS 1
  4.     #define restrict __restrict
  5. #endif
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdint.h>
  11. #include <errno.h>
  12.  
  13. int64_t my_getline(char **restrict line, size_t *restrict len, FILE *restrict fp) {
  14.     // Check if either line, len or fp are NULL pointers
  15.     if(line == NULL || len == NULL || fp == NULL) {
  16.         errno = EINVAL;
  17.         return -1;
  18.     }
  19.     // Use a chunk array of 128 bytes as parameter for fgets
  20.     char chunk[128];
  21.  
  22.     // Allocate a block of memory for *line if it is NULL or smaller than the chunk array
  23.     if(*line == NULL || *len < sizeof(chunk)) {
  24.         *len = sizeof(chunk);
  25.         if((*line = malloc(*len)) == NULL) {
  26.             errno = ENOMEM;
  27.             return -1;
  28.         }
  29.     }
  30.  
  31.     // "Empty" the string
  32.     (*line)[0] = '\0';
  33.  
  34.     while(fgets(chunk, sizeof(chunk), fp) != NULL) {
  35.         // Resize the line buffer if necessary
  36.         size_t len_used = strlen(*line);
  37.         size_t chunk_used = strlen(chunk);
  38.  
  39.         if(*len - len_used < chunk_used) {
  40.             // Check for overflow
  41.             if(*len > SIZE_MAX / 2) {
  42.                 errno = EOVERFLOW;
  43.                 return -1;
  44.             } else {
  45.                 *len *= 2;
  46.             }
  47.             if((*line = realloc(*line, *len)) == NULL) {
  48.                 errno = ENOMEM;
  49.                 return -1;
  50.             }
  51.         }
  52.  
  53.         // Copy the chunk to the end of the line buffer
  54.         memcpy(*line + len_used, chunk, chunk_used);
  55.         len_used += chunk_used;
  56.         (*line)[len_used] = '\0';
  57.  
  58.         // Check if *line contains '\n', if yes, return the *line length
  59.         if((*line)[len_used - 1] == '\n') {
  60.             return len_used;
  61.         }
  62.     }
  63.  
  64.     return -1;
  65. }
  66.  
  67. const char* getfield(char* line, int num) {
  68.     const char* tok;
  69.     for (tok = strtok(line, ","); tok && *tok; tok = strtok(NULL, ",\n")) {
  70.       if (!--num)
  71.           return tok;
  72.     }
  73.     return NULL;
  74. }
  75.  
  76. int main(void) {
  77.     FILE *fp = fopen("ccc.csv", "r");
  78.     if(fp == NULL) {
  79.         perror("Unable to open file!");
  80.         exit(1);
  81.     }
  82.  
  83.     char *line = NULL;
  84.     size_t len = 0;
  85.  
  86.     while(my_getline(&line, &len, fp) != -1) {
  87.         char* tmp = strdup(line);
  88.         printf("Field 4 would be %s\n", getfield(tmp, 4));
  89.         free(tmp);
  90.     }
  91.  
  92.     fclose(fp);
  93.     free(line);
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top