Parsing CSV data into structure - c

I have to put the data from a csv file (name, address, telephone...) to a structure of my C program. It's been unsuccessful, unfortunately. I tried using strtok function to break into tokens every time it finds a ";" (because we're dealing with a Comma Separated File).
Here's what I've done:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_STR_LEN 256
#define MAX_BOOKS 256
struct estrutura
{
int id;
char nome[40];
char endereco[40];
char cidade[40];
char pais[20];
char cep[10];
char nasc[12];
char telefone[14];
char total[20];
};
struct estrutura cliente[200];
FILE *pFile;
//allocate buffer in each line
char *buf = malloc(MAX_STR_LEN);
char *tmp;
void abrir();
/* Functions to be coded
int menu();
int menu2(); //manutencao de clientes
void adicionar();
void alterar();
void excluir();
void exibir();
void pesquisar(); */
main()
{
system("cls");
abrir();
//menu();
}
void abrir() //open the csv file and copy it to
{
/* FileStream for the Library File */
FILE *pFile;
/* allocation of the buffer for every line in the File */
char *buf = malloc(MAX_STR_LEN);
char *tmp;
/* if the space could not be allocated, return an error */
if (buf == NULL) {
printf ("No memory\n");
}
if ( ( pFile = fopen( "control.csv", "r" ) ) == NULL ) //Reading a file
{
printf( "File could not be opened.\n" );
}
int i = 0;
while (fgets(buf, 255, pFile) != NULL)
{
if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == '\n')) //checa leitura
buf[strlen (buf) - 1] = '\0';
tmp = strtok(buf, ";");
cliente[i].id = atoi(tmp); //atoi for int
tmp = strtok(NULL, ";"); //use strcpy for char
strcpy(cliente[i].nome,tmp);
tmp = strtok(NULL, ";");
strcpy(cliente[i].endereco, tmp);
tmp = strtok(NULL, ";");
strcpy(cliente[i].cidade, tmp);
tmp = strtok(NULL, ";");
strcpy(cliente[i].pais, tmp);
tmp = strtok(NULL, ";");
strcpy(cliente[i].cep, tmp);
tmp = strtok(NULL, ";");
strcpy(cliente[i].nasc, tmp);
tmp = strtok(NULL, ";");
strcpy(cliente[i].telefone, tmp);
tmp = strtok(NULL, ";");
strcpy(cliente[i].total, tmp);
//tempBook.ID = atoi(buf); fix below
printf("%i, %s, %s, %s, %s, %s, %s, %s, %s \n",i, cliente[i].id , cliente[i].nome, cliente[i].endereco, cliente[i].cidade, cliente[i].pais, cliente[i].cep, cliente[i].nasc, cliente[i].telefone, cliente[i].total);
i++;
}
//free(buf);
fclose(pFile);
}
How can I solve this problem? I can't successfully copy the data from 100 clients in the csv to a structure.
Thank you since now!

There are three main problems here:
The format string in printf("%i, %s, %s, %s, ...) doesn't match the parameters, you need one more %i: printf("%i, %i, %s, %s, %s, ...).
In your code you never call abrir() but you call menu(), which doesn't exist, therefore your code doesn't even compile.
If you are on Windows (and only then) you need fopen(..., "rt")) instead of fopen(..., "r"))
Furthermore (not causing actual problems in your code):
char *buf = malloc(MAX_STR_LEN); can be replaced by char buf[MAX_STR_LEN];. It's pointless to allocate memory dynamically if the amount of memory is known at compile time. In that case you must of course not call free(buf) (which is commented out anyway).
Following declarations just after struct estrutura cliente[200]; are useless, you can remove them.
FILE *pFile;
//allocate buffer in each line
char *buf = (char*)malloc(MAX_STR_LEN);
char *tmp;
Otherwise the program should work fine unless your input file has fields that are larger than the field in your struct estrutura.

Related

How do I read a comma separated line in a text file and insert its fields into an array of struct pointers?

I've been trying to figure this one out for a while now, and I feel like I have to be close. Basically, I have a data file containing various country records separated by new lines. Each record contains comma separated fields, of which I am trying to extract certain ones.
For example (as a single line):
60,AFG,Afghanistan,Asia,Southern and Central Asia,652090,1919,22720000,45.9,5976,Afganistan/Afqanestan,Islamic Emirate,Mohammad Omar,1,AF
Each one of these lines will make up a struct. Essentially, I want to read each one of these lines and insert it into an array of struct pointers (so dynamically). I also only want specific fields. When I "tokenize" the line I want the fields for code, name, population, and life expec. respectively:
AFG, Afghanistan, 22720000, 45.
My thought was to use fgets() to read each line in the file, and in a loop malloc() some memory for the pointers, tokenize on the fields I want, then insert. However, something that I'm doing must be wrong, as various tests don't seem to show anything in my output.
Here is my work thus far. I would appreciate any and all help.
#include "allheaders.h" // contains all common headers for personal use
#define BUF_SIZE 512
#define NUM_RECS 238
typedef struct {
char code[4];
char name[40];
int population;
float lifeExpectancy;
} Country;
typedef Country *countryPtr;
int main( int argc, const char* argv[] ) {
/* Opening the file */
FILE *filePtr; // pointer to file
if ((filePtr = fopen("AllCountries.dat", "r")) == NULL) { // if couldn't open file
printf("Error opening file\n"); // error message
exit(1);
}
/* Reading the file */
char buffer[BUF_SIZE]; // buffer to read
int index = 0;
char *token;
countryPtr *myCountries = malloc(sizeof(*myCountries) * NUM_RECS);
for(int i = 0; i < NUM_RECS; ++i) {
myCountries[i] = malloc(sizeof(*myCountries[i]));
}
while (fgets(buffer, BUF_SIZE, filePtr) != NULL) {
token = strtok(buffer,",");
token = strtok(NULL, ",");
strcpy(myCountries[index]->code, token);
token = strtok(NULL, ",");
strcpy(myCountries[index]->name, token);
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
myCountries[index]->population = atoi(token);
token = strtok(NULL, ",");
myCountries[index]->lifeExpectancy = atof(token);
//printf("%s", buffer);
index++;
}
printf("%s", myCountries[1]->code); // test?
free(myCountries);
}
Have a look at the following.
In the first instance you will need to do some work to improve the areas marked NYI
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#define BUF_SIZE 512
#define NUM_RECS 238
typedef struct {
char code[4]; // NYI - magic numbers
char name[41]; // NYI - magic numbers
int population; // NYI - what if atoi fails?
float lifeExpectancy; // NYI - what if atof fails?
} Country;
typedef Country* countryPtr;
int main( int argc, const char* argv[] ) {
/* Opening the file */
FILE *filePtr; // pointer to file
if ((filePtr = fopen("a.txt", "r")) == NULL) { // if couldn't open file
printf("Error opening file\n"); // error message
exit(1);
}
/* Reading the file */
char buffer[BUF_SIZE]; // buffer to read
int index=0;
char *token; // NYI - initial value
countryPtr* myCountries = calloc(NUM_RECS, sizeof(countryPtr));
for(int i = 0; i < NUM_RECS; ++i) {
myCountries[i] = calloc(1, sizeof(Country));
}
while (fgets(buffer, BUF_SIZE, filePtr) != NULL) {
// NYI - magic lengths / overflow strcpy targets
token = strtok(buffer,","); // NYI - This is probably not the best way to do this. At least fold into a loop.
token = strtok(NULL, ",");
strcpy(myCountries[index]->code, token);
token = strtok(NULL, ",");
strcpy(myCountries[index]->name, token);
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
token = strtok(NULL, ",");
myCountries[index]->population = atoi(token); // NYI - atoi failure
token = strtok(NULL, ",");
myCountries[index]->lifeExpectancy = atof(token); // NYI - atof failure
printf("%s", buffer);
index++;
}
printf("%s\n", myCountries[0]->code); // test? NYI - need more proof
free(myCountries); // NYI - this is a sequence - need to free each of the new elements
}
I took a different approach to solving it based on your code and data file. I tested it. It works with a file of the record type you showed. Hopefully it will explain some things and make your work easier and give you a good place to work from.
I don't like to write programs in a way that has to pre-count (time consuming) or pre-know the number of records in a file on general principles except maybe in rare cases. So when reading files I prefer to allocate memory as I go. Now if there's a big file and a lot of data, then you have to come up with a better memory management scheme than to keep it all in memory. At some point you're better off going with a canned db solution of some sort. MySQL, an API, library, parser, etc... but this should work for small files.
Usually in C on UNIX, exit(0) means success, exit(-1) means failure. Also since your country codes were 3 characters, the field to hold it has to be at least 4 characters for the trailing '\0'
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#define MAXRECL 512
#define MAXFIELDS 100
#define MAXFIELDL 80
// Field indicies
#define COUNTRY_CODE 1
#define COUNTRY_NAME 2
#define POPULATION 7
#define LIFE_EXPECTANCY 8
#define CCMAX 3
#define CNMAX 40
typedef struct Country {
struct Country *next;
char code[CCMAX + 1]; // (Need room for trailing '\0')
char name[CNMAX + 1]; // (Need room for trailing '\0')
int population;
float lifeExpectancy;
} country_t;
country_t *countryRecords;
int main( int argc, const char* argv[] ) {
FILE *fp;
if ((fp = fopen("AllCountries.dat", "r")) == NULL) {
printf("Error opening file\n");
exit(-1);
}
int totalCountries = 0;
char buf[MAXRECL];
char fields[MAXFIELDS][MAXFIELDL];
country_t *prev_country = NULL;
while (fgets(buf, MAXRECL, fp) != NULL) {
++totalCountries;
country_t *country = calloc(sizeof(struct Country), 1);
if (country == NULL) {
fprintf(stderr, "Out of memory\n");
exit(-1);
}
char *field = strtok(buf, ",");
int i = 0;
while(field != NULL) {
strncpy(fields[i++], field, MAXFIELDL);
field = strtok(NULL, ",");
}
strcpy(country->code, fields[COUNTRY_CODE]);
strcpy(country->name, fields[COUNTRY_NAME]);
country->population = atoi(fields[POPULATION]);
country->lifeExpectancy = atof(fields[LIFE_EXPECTANCY]);
if (countryRecords == NULL)
countryRecords = country;
else
prev_country->next = country;
prev_country = country;
}
printf("Total countries: %d\n", totalCountries);
country_t *country = countryRecords;
while(country != NULL) {
printf("%3s %30s Population: %7d Life Expectancy: %5.2f\n",
country->code, country->name, country->population, country->lifeExpectancy);
country_t *prev_country = country;
country = country->next;
free(prev_country);
}
printf("Done\n");
exit(0);
}

strcpy giving me segmentation fault

I have code which is giving me a segmentation fault. I debugged it and and the error occurred when the strcpy executed. The code is attempting to extract data from a text file and store it into an array of structs. I plan to use strcpy to store the store the text file's data into the structs. Any idea why this is occurring?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int input( char *s, int length);
void main(){
char *tok;
char *buffer;
size_t bufsize = 32;
size_t characters;
FILE *f;
char *file_name;
char line[255];
int currentRoom;
int count = 0;
typedef struct {
char room_n;
char description[100];
char room_north;
char room_south;
char room_west;
char room_east;
} room;
//Creating an array of structs
room record[1000];
while(1){
buffer = (char *)malloc(bufsize * sizeof(char));
if(buffer == NULL){
perror("Unable to allocate buffer");
exit(1);
}
printf("Enter a command: ");
characters = getline(&buffer, &bufsize, stdin);
if(strcmp(buffer,"exit\n") == 0){
printf("Exiting...\n");
exit(1);
}
tok = strtok(buffer, " \n"); // Tokenize input
printf("%s is the token \n", tok);
if (strcmp(tok,"loaddungeon") == 0){
file_name = strtok(NULL, "\n");
printf("file name : %s \n", file_name);
f = fopen(file_name,"r");
while (fgets(line, sizeof(line), f) != NULL)
{
char val1[128];
char val2[128];
char val3[128];
char val4[128];
char val5[128];
char val6[128];
strcpy(val1, strtok(line, "$"));
strcpy(val2, strtok(NULL, "$"));
strcpy(val3, strtok(NULL, " "));
strcpy(val4, strtok(NULL, " "));
strcpy(val5, strtok(NULL, " "));
strcpy(val6, strtok(NULL, " "));
//Segmentation fault error occurs here
strcpy(record[count].room_n, val1);
Definition of strcpy() is:
char *strcpy(char *dest, const char *src)
where-
dest -- This is the pointer to the destination array where the
content is to be copied.
src -- This is the string to be copied.
In your code arguments passed to strcpy() is char and char * :
strcpy(record[count].room_n, val1);
As defined in the structure:
typedef struct {
char room_n; //room_n declared as 'char'
char description[100];
char room_north;
char room_south;
char room_west;
char room_east;
} room;
Suggestion:
Allocate memory for room_n to point to. Change the declaration to
char room_n[128];

reading csv to struct [duplicate]

This question already has answers here:
Reading and parsing lines from a file with fgets and strtok
(2 answers)
Closed 7 years ago.
I am trying to put textfile to the struct
have issues with assigning values form text file to the struct, it keeps printing me the last value being printed incompletely.
Can you kindly help me out with the issue?
Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 422498
#define MAX_KEY 128
struct record
{
char* a;
char* b;
};
int main(int argc, char **argv){
char* input=argv[1];
char* output=argv[2];
char* buff[MAX_LEN];
char *delimiter = ";";
//printf("%s\n",input);
//printf("%s\n",output);
FILE *fp;
fp = fopen(input, "r"); //opening file*/
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
int rowsNum=0;
struct record* recArr = malloc(3 * sizeof (struct record));
//struct record recArr[4];
//recArr=malloc(1 * 2*sizeof(char*));
/*recArr[0].a="aa";
recArr[0].b="bb";
recArr[1].a="cc";
recArr[1].b="dd";
recArr[2].a="ee";
recArr[2].b="ff";
printf("%s\n", recArr[0].a);
printf("%s\n\n", recArr[0].b);
printf("%s\n", recArr[1].a);
printf("%s\n\n", recArr[1].b);
printf("%s\n", recArr[2].a);
printf("%s\n\n", recArr[2].b);*/
while (fgets(buff, MAX_LEN, (FILE*)fp)!=NULL)
{
//recArr=realloc(recArr, (rowsNum+1) * 2*sizeof(char*));
//char* Key = strtok(buff, delimiter);
//char* Value = strtok(NULL, delimiter);
recArr[rowsNum].a=strtok(buff, delimiter);
recArr[rowsNum].b=strtok(NULL, delimiter);
printf("%s\n", recArr[rowsNum].a);
printf("%s\n\n", recArr[rowsNum].b);
/*Key=NULL;
Value=NULL;*/
rowsNum++;
printf("%d\n", rowsNum);
}
fclose(fp);
int i;
for (i=0;i<3;i++)
{
printf("%s\n", recArr[i].a);
printf("%s\n\n", recArr[i].b);
}
//printf("%d\n", rowsNum);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // this is needed to use strtok function (and strlen, strcpy)
#define MAX_LEN 422498
#define MAX_KEY 128
struct record
{
char* a;
char* b;
};
int main(int argc, char **argv){
char* input=argv[1];
char* output=argv[2];
char buff[MAX_LEN]; // buff should be char[MAX_LEN], not char*[MAX_LEN]
char *delimiter = ";";
//printf("%s\n",input);
//printf("%s\n",output);
FILE *fp;
fp = fopen(input, "r"); //opening file*/
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
int rowsNum=0;
struct record* recArr = malloc(3 * sizeof (struct record));
//struct record recArr[4];
//recArr=malloc(1 * 2*sizeof(char*));
/*recArr[0].a="aa";
recArr[0].b="bb";
recArr[1].a="cc";
recArr[1].b="dd";
recArr[2].a="ee";
recArr[2].b="ff";
printf("%s\n", recArr[0].a);
printf("%s\n\n", recArr[0].b);
printf("%s\n", recArr[1].a);
printf("%s\n\n", recArr[1].b);
printf("%s\n", recArr[2].a);
printf("%s\n\n", recArr[2].b);*/
while (fgets(buff, MAX_LEN, (FILE*)fp)!=NULL)
{
//recArr=realloc(recArr, (rowsNum+1) * 2*sizeof(char*));
//char* Key = strtok(buff, delimiter);
//char* Value = strtok(NULL, delimiter);
//recArr[rowsNum].a=strtok(buff, delimiter);
//recArr[rowsNum].b=strtok(NULL, delimiter);
// you have to COPY the string
char* a=strtok(buff, delimiter);
char* b=strtok(NULL, delimiter);
recArr[rowsNum].a = malloc(sizeof(char) * (strlen(a) + 1)); // +1 for terminating '\0'
recArr[rowsNum].b = malloc(sizeof(char) * (strlen(b) + 1));
strcpy(recArr[rowsNum].a, a);
strcpy(recArr[rowsNum].b, b);
printf("%s\n", recArr[rowsNum].a);
printf("%s\n\n", recArr[rowsNum].b);
/*Key=NULL;
Value=NULL;*/
rowsNum++;
printf("%d\n", rowsNum);
}
fclose(fp);
int i;
for (i=0;i<3;i++)
{
printf("%s\n", recArr[i].a);
printf("%s\n\n", recArr[i].b);
}
//printf("%d\n", rowsNum);
// you should free the allocated buffer
for (i=0;i<3;i++)
{
free(recArr[i].a);
free(recArr[i].b);
}
free(recArr);
}
string.h should be included to use strtok : GCC made some warnings about it.
the type of buff was not proper: it should be char buff[MAX_LEN], not char* buff[MAX_LEN](an extra asterisk is there)
the function strtok modifies the buffer given and returns the pointer that is pointing somewhere in the buffer. When reading the next line, the buffer is overwritten and the text previously read is lost unless it is copied to somewhere. For that reason, I added some code that copies the strings read and save pointers that points where the strings are copied instead of somewhere in buff.

reading a csv file into struct array

I'm beginning to code in C. My code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_LEN 256
#define MAX_BOOKS 256
struct book{
int ID;
char *name;
char *dateIn;
char *dateOut;
};
struct book books[MAX_BOOKS];
/* PROTOTYPE OF FUNCTIONS */
int readBookFile();
void printBookList();
int main(int argc, char **argv)
{
int isOK = 0;
isOK = readBookFile();
printBookList();
system("pause");
return 0;
}
int readBookFile()
{
/* FileStream for the Library File */
FILE *bookFile;
/* allocation of the buffer for every line in the File */
char *buf = malloc(MAX_STR_LEN);
char *tmp;
/* if the space could not be allocaed, return an error */
if (buf == NULL) {
printf ("No memory\n");
return 1;
}
if ( ( bookFile = fopen( "library.dat", "r" ) ) == NULL ) //Reading a file
{
printf( "File could not be opened.\n" );
}
int i = 0;
while (fgets(buf, 255, bookFile) != NULL)
{
if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == '\n'))
buf[strlen (buf) - 1] = '\0';
tmp = strtok(buf, ";");
books[i].ID = atoi(tmp);
tmp = strtok(NULL, ";");
books[i].name = tmp;
tmp = strtok(NULL, ";");
books[i].dateIn = tmp;
tmp = strtok(NULL, ";");
books[i].dateOut = tmp;
//tempBook.ID = atoi(buf);
printf("index i= %i ID: %i, %s, %s, %s \n",i, books[i].ID , books[i].name, books[i].dateIn , books[i].dateOut);
i++;
}
//free(buf);
fclose(bookFile);
return 0;
}
void printBookList()
{
int i;
//i = sizeof(books) / sizeof(books[0]);
//printf ("%i \n", i);
for (i = 0; i <= sizeof(books); i++)
{
if (books[i].ID != 0)
printf("index i= %i ID: %i, %s, %s, %s \n",i, books[i].ID , books[i].name, books[i].dateIn , books[i].dateOut);
else
break;
}
}
The problem is, that after readBookFile() ends, the Array of my struct is full of the last value of the input file..
My input file is:
1;das erste Buch; 12122013; 13122013
2;das Zweite Buch; 12122013; 13122013
3;das dritte Buch; 12122013; 13122013
4;das vierte Buch; 12122013; 13122013
5;das fünfte Buch; 12122013; 13122013
6;das sechste Buch; 12122013; 13122013
so in the readBookFile function the printf returns the correct values, but in the printBooksList() function all values seem to have changed to the last line of my inputfile.
Can anyone explain this to me and maybe point me in the right direction?
Thanks a lot
Hagbart
The problem is your struct:
struct book{
int ID;
char *name;
char *dateIn;
char *dateOut;
};
name, dateIn, dateOut are "pointer", they are just point to something, you're not allocating spaces for them.
What you do is just point them to tmp(buf).
So what you do in printBookList() is just print same string block, while ID is OK since it's not pointer.
To solve this, allocate space for them, you can use strdup(), but make sure to free them.
In the while loop:
while (fgets(buf, 255, bookFile) != NULL)
you are copying into the memory location of buffer new contents from file. As tmp points to a certain point in the buffer, its contents are being replaced too.
tmp = strtok(NULL, ";");
books[i].name = tmp;
You should allocate memory for each struct of the array and then use strcopy.
You can find an explanation of differences between strcpy and strdup here:
strcpy vs strdup
The reason is that in something like
books[i].name = tmp;
You're not actually copying a string from tmp into books[i].name: you just make both point to the same location - somewhere into the buf buffer.
Try using strdup instead, as in:
books[i].name = strdup(tmp);

Want to free my pointer token after strtok

I have extracted the "meaning" part of my code (and also replace some line to simplify it).
I have 2 dynamic pointers, one for the current line (extracted from a file) and a second for the current token.
Following this question, Free/delete strtok_r pointer before processing complete string?
I wrote this :
int main(void) {
int n = 455;
char *tok2, *freetok2;
char *line, *freeline;
line = freeline = malloc(n*sizeof(*line));
tok2 = freetok2 = malloc(n*sizeof(*tok2));
/* content of the file) */
const char* file_reading = "coucou/gniagnia/puet/";
/* reading from the file */
strcpy(line, file_reading);
strtok(line, "/");
/* get the second token of the line */
tok2 = strtok(NULL, "/");
fprintf(stdout, "%s \n", tok2); // print gniagnia
fprintf(stdout, "%s \n", line); // print coucou
/* free error */
//free(tok2);
/* worked, but maybe don't free "everything ?" */
//free(line);
free(freetok2);
free(freeline);
return 0;
}
But at the end, I'm not sure of what is correct or not, and I find this solution not so elegant (because of using 2 "save variables".
Is that correct ? Is there some ways to improve it ?
Thanks
Edit: changed my code for this, (and it will handle all the lines of the file)
include <unistd.h>
include <stdlib.h>
int main(void) {
char *tok2;
char *line;
/* content of the file) */
const char* file_reading = "coucou/gniagnia/puet/";
const char* file_reading2 = "blabla/dadada/";
/* reading from the file */
line = strdup(file_reading);
strtok(line, "/");
/* get the second token of the line */
tok2 = strtok(NULL, "/");
printf("%s \n", tok2);
printf("%s \n", line);
/* reading from the file */
line = strdup(file_reading2);
strtok(line, "/");
/* get the second token of the line */
tok2 = strtok(NULL, "/");
printf("%s \n", tok2);
printf("%s \n", line);
free(line);
return 0;
}
You're not actually using the memory pointed by freetok2, you don't need to malloc anything, thus you don't need the freetok2 variable.
Saying free(line) or free(freeline) is the same in your code so you don't need the freeline at all.
Another problem is this: malloc(n*sizeof(*line));. You might as well be saying: malloc(n); because sizeof(char) is always 1. But best of all would be:
line = malloc(strlen(file_reading) + 1);
strcpy(line, file_reading);
The code should be modified as follows:
int main(void) {
int n = 455;
char *tok2;
char *line;
line = malloc(n*sizeof(*line));
/* content of the file) */
const char* file_reading = "coucou/gniagnia/puet/";
/* reading from the file */
strcpy(line, file_reading);
strtok(line, "/");
/* get the second token of the line */
tok2 = strtok(NULL, "/");
fprintf(stdout, "%s \n", tok2); // print gniagnia
fprintf(stdout, "%s \n", line); // print coucou
free(line);
return 0;
}

Resources