Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This will only have effect on Windows with MSVC
- #ifdef _MSC_VER
- #define _CRT_SECURE_NO_WARNINGS 1
- #define restrict __restrict
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <errno.h>
- int64_t my_getline(char **restrict line, size_t *restrict len, FILE *restrict fp) {
- // Check if either line, len or fp are NULL pointers
- if(line == NULL || len == NULL || fp == NULL) {
- errno = EINVAL;
- return -1;
- }
- // Use a chunk array of 128 bytes as parameter for fgets
- char chunk[128];
- // Allocate a block of memory for *line if it is NULL or smaller than the chunk array
- if(*line == NULL || *len < sizeof(chunk)) {
- *len = sizeof(chunk);
- if((*line = malloc(*len)) == NULL) {
- errno = ENOMEM;
- return -1;
- }
- }
- // "Empty" the string
- (*line)[0] = '\0';
- while(fgets(chunk, sizeof(chunk), fp) != NULL) {
- // Resize the line buffer if necessary
- size_t len_used = strlen(*line);
- size_t chunk_used = strlen(chunk);
- if(*len - len_used < chunk_used) {
- // Check for overflow
- if(*len > SIZE_MAX / 2) {
- errno = EOVERFLOW;
- return -1;
- } else {
- *len *= 2;
- }
- if((*line = realloc(*line, *len)) == NULL) {
- errno = ENOMEM;
- return -1;
- }
- }
- // Copy the chunk to the end of the line buffer
- memcpy(*line + len_used, chunk, chunk_used);
- len_used += chunk_used;
- (*line)[len_used] = '\0';
- // Check if *line contains '\n', if yes, return the *line length
- if((*line)[len_used - 1] == '\n') {
- return len_used;
- }
- }
- return -1;
- }
- const char* getfield(char* line, int num) {
- const char* tok;
- for (tok = strtok(line, ","); tok && *tok; tok = strtok(NULL, ",\n")) {
- if (!--num)
- return tok;
- }
- return NULL;
- }
- int main(void) {
- FILE *fp = fopen("ccc.csv", "r");
- if(fp == NULL) {
- perror("Unable to open file!");
- exit(1);
- }
- char *line = NULL;
- size_t len = 0;
- while(my_getline(&line, &len, fp) != -1) {
- char* tmp = strdup(line);
- printf("Field 4 would be %s\n", getfield(tmp, 4));
- free(tmp);
- }
- fclose(fp);
- free(line);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement