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()
function? Something of the form
fscanf(input, "%lf %lf", ...code..., ...code...);
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)