简体   繁体   中英

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

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.

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);            
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM