Это другой подход, который работает и для больших файлов.
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* aux_slice(const char* str, char delimiter, const char** pRet);
char **split(const char *str, char delimiter, int *ret_size);
int main()
{
int count = 0, i = 0;
char** splits = split(",,, 1,2,3,4,5,6,7,8,9,10, aaaa, a,a a,aa,,a,,,,a,a a,a a,a,,,,,ashajhsas asjas,,a,a,,aa"
"aaaaaaaaaaa.......,p,p,p,p,p,p,p, this is last,,,,,,,,,,", ',', &count);
printf("Strings (%d)\n", count);
for (i=0 ; i < count; i++) {
printf("%s\n", splits[i]);
}
for(i=0; i < count; i++) {
free(splits[i]);
}
free(splits);
return 0;
}
char* aux_slice(const char* str, char delimiter, const char** pRet)
{
int size = 0, i = 0;
const char* begin = str;
char *ret = NULL;
int match = 0;
if (!str) {
return NULL;
}
while (*begin != '\0') {
if (*begin == delimiter) {
match++;
break;
}
size++;
begin++;
}
ret = (char*)malloc(sizeof(char) * size);
if(ret == NULL) {
return NULL;
}
if (match) {
/* we have a delimiter ??? */
for(i = 0; str[i] != delimiter; ++i) {
ret[i] = str[i];
}
ret[i] = '\0';
while (*begin == delimiter) {
begin++;
}
(*pRet) = begin;
} else {
/* or we just copy the remaining string.... */
for(i=0; str[i] != '\0'; ++i) {
ret[i] = str[i];
}
ret[i] = '\0';
(*pRet) = NULL;
}
return ret;
}
char **split(const char *str, char delimiter, int *ret_size)
{
int diff = 0, splits = 0, i=0;
const char* begin = str;
const char* end = &str[strlen(str)-1];
while (*begin == delimiter) begin++;
while (*end == delimiter) end--;
diff = (end - begin)+1;
while (i < diff) {
// avoid cases of adjacent delimiters
// like "str1,str2,,,,,str3
if (begin[i] == delimiter) {
while (begin[i] == delimiter) i++;
splits++;
}
i++;
}
splits += 1;
*ret_size = splits;
char** split_str = (char**)malloc(sizeof(char**)*splits);
if (split_str == NULL) {
return NULL;
}
for(i=0; i < splits; ++i) {
split_str[i] = aux_slice(begin, delimiter, &begin);
}
return split_str;
}
Демо: https://onlinegdb.com/BJlWVdzGf