Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
The fprintf inserts the sum/200 later than it is supposed to and part of the original file disappears. I imagine that it has to with timing of the various steps. Any way I can make this work?
Thanks

Code:
 for(l = 0; l < numstocks; l++)
                {
                rewind(stocks[l]);
                sum = 0;
                for (i = 0; i < 200; i ++)
                    {
                    k = fscanf(stocks[l],"%s %f ", date, price[i]);
                    sum += *price[i];
                    }
                fprintf(stocks[l],"     %6.3f\n", sum/200); 
              
                }
 
maybe fflush? Or fclose to force a flush if you're done with the file?

Personally, I'd do it as two operations if I understand what is supposed to happen. open the file read only, and read it, close the file and open the file for append and append the summary line.

If that's what's supposed to happen.

B
 
Personally, I'd do it as two operations if I understand what is supposed to happen. open the file read only, and read it, close the file and open the file for append and append the summary line.

If that's what's supposed to happen.

B

That seems plausibly what it is trying to do and what I also gather from the OP :)
 
thanks guys.
If I use ftell and fseek, I can get the float inserted in the right place but I still overwrite part of next line. I need to use a second buffer to store the end of the file and then rewrite it back into the original file.
 
Out of curiousity, how many files do you have open simultaneously here, 'numstocks' I presume, but how many is that?
 
right now - 50.
Instead of reading the data in and then going back do the calculations, I am doing it all at once.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.