archive - how can i read a .txt file separated by commas in C? -
i'm having lot of problems doing task. have txt 1 line of words separated commas. have read , put in array. far tried using strtok() gives me errors. here's code:
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<omp.h> int main(){ char string[5000],list[200],*token,s=", "; int i; file *lista; lista=fopen("lista.txt","r"); i=0; token=strtok(string,s); strcpy(list[i],token); while(fscanf(lista,"%s",string)!=eof){ token=strtok(null,s); strcpy(list[i],token); i=i+1; } fclose(lista); }
it gives me the" expectig char *restrict" error i'm out of ideas. btw: i'm in linux
there many strange things in code, guess, want this:
char string[5000], *list[200], *token; char * s = ","; int i; file *lista; lista = fopen("c:\\file.txt", "r"); int max_file_size = 1000; char * buffer = (char*)malloc(sizeof(char)*max_file_size); fread(buffer, sizeof(char), max_file_size, lista); list[0] = strtok(buffer, s); (int = 1;; i++) { list[i] = strtok(null, s); if (list[i] == null) { break; } } fclose(lista);
what strange/wrong in code:
- you passing
char* string
strtok function, variable uninitialised when passing - you have file pointer
lista
, never read file - you have variable
list
array of 200 chars, guess want have variablelist
list of strings - strtok eats 2 parameters, const char* inputstring , const char* delimiter. variable
s
should const char *
Comments
Post a Comment