用的fscanf冒号(:)分隔的数据 [英] fscanf with colon (:) delimited data

查看:459
本文介绍了用的fscanf冒号(:)分隔的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何的fscanf 这一块的数据?有数据和分隔符之间没有空行是

  VS1234567890654327:罗布Fordfirst:001:200
VS1234567890654312:史蒂芬哈珀:200:010

我的code

 而(3 ==的fscanf(文件名,????,和放大器;字符串[尺寸],和放大器;名称[尺寸],和放大器;数字1【尺寸】,和放大器;号码2 [大小])){
    //的printf(%S - %S - %.3D - %.3D,串【尺寸】,姓名【尺寸】,数字1【尺寸】1,数字[尺寸]));
    大小++;
}


解决方案

您可以包括分隔符作为格式的一部分的fscanf ,就像这样:

 而(4 ==的fscanf(文件名,%[^:]:%[^:]:%D:%D,串【尺寸】,姓名【尺寸】 ,&安培;数字1【尺寸】,和放大器;数字2 [大小])){
    ...
}

请注意使用%[^:]的格式说明。它说:除了任意字符:被接受。还要注意的是的char * 参数都没有符号传递,因为它们已经指针。

演示上ideone。

How do I fscanf this piece of data? There are no empty rows between the data and delimeter is a ':'

VS1234567890654327:Rob Fordfirst:001:200
VS1234567890654312:Steven Harper:200:010

my code

while(3==fscanf(filename, "????", &string[size], &name[size], &number1[size], &number2[size])) {
    //printf("%s - %s - %.3d - %.3d", string[size], name[size], number1[size], number2[size]));
    size++;
}

解决方案

You can include the delimiters as part of your format to fscanf, like this:

while (4 == fscanf(filename, "%[^:]:%[^:]:%d:%d", string[size], name[size], &number1[size], &number2[size])) {
    ...
}

Note the use of %[^:] format specifier. It says "any character except ':' is accepted". Also note that char* parameters are passed with no ampersand, because they are already pointers.

Demo on ideone.

这篇关于用的fscanf冒号(:)分隔的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆