Hi g
I was just wondering how to do this because it's killing me.
In the "C" Language
With fscanf how can you possibly take a text file with the contents:
"a b 10 20 30 67
a b 20 30 77 64"
Where "a" and "b" are names and the numbers at the end have no limit to how many?
So that you can have a variable to store each name and each of the numbers?
fscanf("%s %s %d etc.") does not work because there is two lines
install BString (better string library)
read in the file as a bstring
split the bstring by the newline character
use fscanf on each line
If the count of numbers is variable, you can't really do it with fscanf, unless you define a maximum count of numbers. Functions that deal with formatted text typically return the number of elements that matched the template string. You can use that number to determine how many numbers you parsed. But this is still a fixed approach. For a better approach, consider parsing the lines yourself, which is easy if you use strtok.
>>51478978
Or just use fgets? What the fuck is wrong with millennial programmers, shit son.
>>51478880while ( fscanf("%s %s %d %d %d %d",a, b, &c, &d, &e, &f) == 6) {
// stuff here
}
>>51479037
You're just missing the read line-by-line code
>>51479037
so what if you don't know how many numbers at the end there are and it isn't just 4 %d's
Each newline there is a different number e.g.
Pretend the file is:
"string string num num num
string string num num
string string
string string num num num num num num"
but you don't know what the file looks like or how many of each thing there is except two strings.
But there can't be more than 10 on any line
>>51479007
>fgets
>>51479119
>Pretend the file is:
>"string string num num num
>string string num num
>string string
>string string num num num num num num"
OP said
>With fscanf how can you possibly take a text file with the contents:
>
>"a b 10 20 30 67
>a b 20 30 77 64"
so i assumed the file was that
>>51479119
Change the condition to (> 2) and use the return value to determine how many numbers you parsed (matches - 2).
>>51479148
sorry but how would you do it assuming you don't know what the file is.
Also there are multiple lines of text as shown in the "pretend" example.
>>51479123
You are mistaken.
gets is deprecated
fgets is not deprecated as of C11.