[英]getting an input with specific format in c
我正在嘗試用C語言讀取文件!
文件格式為:(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)
等等
我有以下代碼,但不幸的是此代碼無法正常工作! 問題是我所有的行都進入了dest_ip變量,其余的變量對於每行都為空!
有人可以幫我嗎? 謝謝
//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
這是評論中的Chux解決方案,並且運行完美!!!!
考慮char dest_ip [16]; 字符source_ip [16]; 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.