tags:

views:

101

answers:

6

A quick question, is there a way to perform these 3 operations:

while(...) {
fscanf(input, "%lf %lf", &t, &y);
tTotal += t;
yTotal += y;
}

in one operation where t and y add themselves to tArray and yArray respectively, inside the scanf statement? Something of the form

fscanf(input, "%lf %lf", ...code..., ...code...); 

Thanks, Ash.

A: 

In C++ this would be fairly easy, but C it's likely to be pretty challenging (at best).

Jerry Coffin
+1  A: 

Pointer arithmetic to the rescue!

while(fscanf(input, "%lf %lf", t, y)) {t++; y++;}

Where t and y point to the start of their arrays initially.

Of course, you'll need to add your own bounds checks.

Anon.
That doesn't seem to perform the requested addition.
Shmoopty
I think that's actually wrong, because he wants to add the scanned values to the pre-existing, not just assign them.
3lectrologos
Yes sorry the code wasn't completely clear, I've edited it now.
Ash
+2  A: 

Doing that would require fscanf() to know the operation that you wanted in addition to where to store the result (and it would be undefined in the case where the variable hadn't yet been initialized).

You could write a wrapper around fscanf() to do it, but that's just a lot of work for a very specific use case, and would ultimately be slower than just doing the += after.

Seth
+2  A: 

It is not possible. fscanf simply writes the scanned values to the memory locations given by the pointers, and there is no way to make that operation perform any addition.

jk
I actually made it perform an addition, albeit it behaves a bit wrong. Declared a global int i and a function int* add(int* a) {i=+*a; return a;}, then called scanf("%d",add( In a for loop, the addition I wanted is performed exactly one iteration after the data is provided :D This code is WRONG, but I had to try :D
mingos
That's because function arguments are evaluated before the function is called, so your `add` gets called before `scanf`.
jk
+2  A: 

There is no way to put the addition into the parameters of the fscanf function because fscanf accepts pointers and simply writes to their locations. However, you could still make the addition part of the same statement like this:

while(...) {
    fscanf(input, "%lf %lf", &t, &s), tTotal += t, sTotal += s;
}

Note that the result of this statement is the value of the last expression. So, for example, if you wanted to record the return value of scanf, you should do:

while(...) {
    int res;
    useResult((res = fscanf(input, "%lf %lf", &t, &s), tTotal += t, sTotal += s, res));
}

... which is ugly but works.

(Disclaimer: I haven't compiled this code)

antonm
A: 

The question is: what are you trying to achieve?
If you just want to reduce clutter, create an inline function something like:

int GetData(FILE *fp, double *ptTotal, double *pyTotal)
{
    double t, y;
    if (2 == fscanf(fp, "%lf %lf", &t, &y))
    {
         *ptTotal += t;
         *pyTotal += y;
         return 1;
    }
    else
    {
        return 0;
    }
}

and call that in a loop.

However if you have hopes of speeding up the loop, then I don't like your chances. fscanf is probably many times slower than the maths, so I don't think you'll speed it up much without switching to a bulk reading style, which will make the program much more complex.

Michael J