简体   繁体   English

在c中获取具有特定格式的输入

[英]getting an input with specific format in c

I am trying to read a file in C language! 我正在尝试用C语言读取文件!

The format of the file is this: (IP address,IP address,cost) for example: 文件格式为:(IP地址,IP地址,成本),例如:

(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! 问题是我所有的行都进入了dest_ip变量,其余的变量对于每行都为空!

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!!!!! 这是评论中的Chux解决方案,并且运行完美!!!!

Consider char dest_ip[16]; 考虑char dest_ip [16]; char source_ip[16]; 字符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 if(fscanf(fp,“(%15 [0-9。],%15 [0-9。],%d)”,dest_ip,source_ip,&cost)== 3)....这将输入限制为15 char并且不会像“%[^,]”那样以不寻常的'\\ 0'进行扫描

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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