[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Hi g I was just wondering how to do this because it's killing
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 12
Thread images: 2
File: circut.png (36 KB, 1105x585) Image search: [Google]
circut.png
36 KB, 1105x585
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.
>>
>>51478880

while ( 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
>>
File: 3678.deprecated.png (151 KB, 1000x379) Image search: [Google]
3678.deprecated.png
151 KB, 1000x379
>>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.
Thread replies: 12
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.