I'm trying to scan a line from a data file(txt format) it just contains a line of ints ex.
3457983257892

I need to scan the line and get the total number of individual ints.

Basically, 34563456 would total to 8. I'm not sure how to manipulate a while loop using fscanf and increment variable i. I think its that simple but it keeps core dumping.

What I have so far.

i = 0;
    while(fscanf(dFile, "%1d")!= EOF) {
        i++
    }

Recommended Answers

All 3 Replies

Well you're not using fscanf correctly, so no wonder it blows up.

int val;
while ( while(fscanf(dFile, "%1d", &val ) == 1 )

Not sure why you have two while loops? This is what I have so far and pretty much works except for the first number it reads is 1628741220

The first three numbers should be 1 5 2 (the rest are correct) but it goes
1628741220 2 3.

Here is what I have for code.

i = 0;
    int val;
    while(fscanf(dFile, "%1d", &val ) == 1 ,fscanf(dFile, "%1d", &val ) != 1 , i++)

I couldn't figure out how to incorporate your second while loop though...

> Not sure why you have two while loops?
Just copy/paste - nothing more

And your while loop is far too complex.
Put most of the code into the body of the loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.