简体   繁体   中英

getting an input with specific format in c

I am trying to read a file in C language!

The format of the file is this: (IP address,IP address,cost) for example:

(127.0.0.1,128.1.11.12,12)
(127.0.1.2,128.0.1.12,12)
(127.0.3.4,128.4.0.12,12)

and etc

I have the following code but unfortunately this code is not working! The problem is all of my line is going into the dest_ip variable and the rest of the variables are going to be null for each line!

Can someone please help me with this? Thanks

//open the fp and etc
char * source_ip;
char * dest_ip;
int cost;
int counter = my_ips_size;
printf("I am about to read\n");
while (fscanf(fp,"(%s,%s,%d)",dest_ip,source_ip,&cost) != EOF) { //do what ever

This is the Chux solution in the comment and it is working perfect!!!!!

Consider char dest_ip[16]; char source_ip[16]; if(fscanf(fp," (%15[0-9.],%15[0-9.],%d)",dest_ip,source_ip,&cost)== 3) .... This limits input to 15 char and does not scan in an unusual '\\0' like "%[^,]" does

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