且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用scanf函数\\的fscanf读取一行并解析到变量?

更新时间:2023-11-22 13:07:34

假设你有:

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.