如何使用scanf函数\\的fscanf读取一行并解析到变量? [英] How to use scanf \ fscanf to read a line and parse into variables?
问题描述
我要看书,在每一行的以下格式内置的文本文件:
I'm trying to read a text file built with the following format in every line:
的char *,字符*,诠释
char*,char*,int
即:
AAAAA,dfdsd,23
aaaaa,dfdsd,23
bbbasdaa,国内长途,100
bbbasdaa,ddd,100
我想使用的fscanf读取文件中的一行,并自动解析行成varilables字符串1,字符串,INTA
i want to use fscanf to read a line from file, and automatically parse the line into the varilables string1,string2,intA
什么是做它的正确方法是什么?
谢谢
What's the correct way of doing it ? Thanks
推荐答案
假设你有:
char string1[20];
char string1[20];
int intA;
你可以这样做:
fscanf(file, "%19[^,],%19[^,],%d\n", string1, string2, &intA);
%[^,]
读的非逗号字符的字符串,并在第一个逗号停止。 19
是一个字符阅读(假设20的缓冲大小),因此您不必缓冲区溢出的最大数量。
%[^,]
reads a string of non-comma characters and stops at the first comma. 19
is the maximum number of characters to read (assuming a buffer size of 20) so that you don't have buffer overflows.
这篇关于如何使用scanf函数\\的fscanf读取一行并解析到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!