0

so i have this simple program where i have to load values into arrays in another function, have csv file with some data of people randomly generated, separated by ; and need to load into 3 separate arrays

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//not all necessary just the usual i put at the beggining

int main()
{
char name[100];
char surname[100];
int birth[100];

load_values(name,surname,birth);
}

int load_values(name,surname,birth)
   FILE *data;
   data = fopen("list_of_values.csv","r");
   char letter;
   while(letter = getc(data)) != EOF){ //using this to go trough the file, yes it is quite bad also need help :D
       fgets(data,"%s","%s","d",&name,&surname,&birth) ///reads the file by lines and puts walues into arrays?
   return (name,surname,birth);
}
list_of_values.csv look like this
Tom Brombadil;1997
Joh-Bob Larson;1999
Evan Thompson;1899
//probably the ; will be a problem too :/

expecter result is for the arrays to hold values like:

name[Tom,Joh-Bob,Evan]
surname[Brombadil,Larson,Thompson]
birth[1997,1999,1899]
D. K.
  • 137
  • 10
  • 1
    Hello, please post a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), the shortest *compilable* code that shows what you have tried. The code posted has so many errors it is hard to know where to begin. – Weather Vane Nov 10 '21 at 17:56
  • Nitpick and not your problem, but: CSV stands for *comma* separated values. So this is not really a CSV file. – Steve Summit Nov 10 '21 at 17:57
  • 1
    If you've got `char name[100]`, that will let you read one name of up to 99 characters. If you're intending to be able to read multiple names (and surnames and birth dates), you are going to need a different data structure. – Steve Summit Nov 10 '21 at 17:59
  • Using a `char` variable to hold `getc`'s return value is wrong; it needs to be `int`. (But calling `getc` in the first place is wrong here, so it doesn't really matter.) – Steve Summit Nov 10 '21 at 18:01
  • This previous question [Read .csv file in C](https://stackoverflow.com/questions/12911299/read-csv-file-in-c) may give you some ideas. – Weather Vane Nov 10 '21 at 18:02

1 Answers1

1

It seems that you are stuck at this point:

   while(letter = getc(data)) != EOF){ //using this to go trough the file, yes it is quite bad also need help :D
       fgets(data,"%s","%s","d",&name,&surname,&birth) ///reads the file by lines and puts walues into arrays?

getc and fgets work in different ways:

  • getc consumes a byte

  • fgets consumes a buffer of n bytes

Don't mix them, to scan a line from the csv you want something like:

char buf[1024];

while (fgets(buf, sizeof buf, data))
{
    if (sscanf(buf,"%99s,%99s,%d", name, surname, &birth) != 3)
    {
        fprintf(stderr, "Wrong format. Expected = <string> <string> <int>\n");
        exit(EXIT_FAILURE):
    }
}

Notice that %99s is useful to avoid buffer overflows.

But as pointed out by @SteveSummit in a comment, you have space for only one line in the strings:

char name[100];    // Space for 1 line
char surname[100]; // Space for 1 line
int birth[100];    // Space for 100 lines

If you want to store an array of lines you need another structure, i.e. a linked list or a dynamic allocated array using realloc.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94