How to split line I read from file with delimiters and store them in each int variable in C

67 Views Asked by At

Hi I'm completely new to C and used to code in python so this has been bothering me for a while but I couldn't find an answer that I've been looking for so I decided to ask here.

I am reading a file called "input_date.txt" and will have to split it with delimiter / and store each part into different int variable. Here's what I have so far:

int mm, dd, yy;

FILE* fp = fopen("input_date.txt", "r");

In the input_date.txt file there is only one line

5/17/07

I'll have to split them and store 5 in mm, 17 in dd and 07 in yy (all int variables). I kinda figured that I can use strsep but I'm still not too sure how to work with it (I barely started learning C) so any help would be appreciated.

1

There are 1 best solutions below

4
On

fscanf might do what you want?

if (fscanf(fp, "%d/%d/%d",&mm,&dd,&yy) != 3) {
    /* Handle error */
    fputs ("error: invalid date format.\n", stderr);            
}