Right. Read the documentation on fread. What does fread return?
Right. Read the documentation on fread. What does fread return?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
This is wrong... in so many ways.
You're only reading in every other number into array, because fread() grabs everything else and puts it into an unused variable. Then, you try to fscanf() to the entire array, instead of incrementing an array. It should be more like this:
where "i" is a variable that is incremented. Bringing us to the next problem: using an infinite loop and breaking is usually poor design, as shown here. If you use this construct, you won't have a counter variable. Edit: And to clarify, you don't need the fread() at all, because fscanf() also returns the amount of parameters successfully read in.Code:fscanf(fp, "%f", &array[i]);
And potentially the biggest issue, you're trying to read a 4-byte floating point value into an array of 200 1-byte fixed-point values. If you get to this point successfully and without crashing, the data certainly won't be anything useful.
Finally, you should take a look at these articles:
- SourceForge.net: Pause console - cpwiki
- SourceForge.net: Indentation - cpwiki
and fix your code before posting back.
Get rid of feof(), make the array of floats (not chars), and make sure you initialize i to zero first.
You also need to use a loop to print, and use the right format specifiers:Code:int i = 0; float array[100]; while (i < 100) { if (fscanf(fp, "%f", &array[i]) != 1) break; i++; }
Code:int j; for (j = 0; j < i; j++) { printf("%f, ", array[j]); } /* I know for() loops are expensive, but it's for demonstration purposes anyway */
Err... they aren't, at least no more than the roughly equivalent while loop.Originally Posted by memcpy
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
This looks wrong:
Code:while(i==100)
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Post your code, I have no idea what you're doing. And while you're at it, drop the system("pause") again, and don't put it back.
Stop selectively listening to us and POST YOUR FULL CODE.
Code:#include <stdio.h> #include <stdlib.h> int main() { FILE *fp; int i=1,k,j; float numbers,average=0,now,temp,minimum; if ( (fp=fopen("numbers.dat","wb+"))==NULL) { printf ("No such a file \n"); system("pause"); exit(1); } printf ("Enter number\n"); printf ("End ctrl+z \n"); while (!feof(stdin)) { printf("Enter %d number : \n",i); fscanf(stdin, "%f", &numbers); if(feof(stdin)) { break; } fwrite(&numbers, sizeof(float),1,fp); i++; } i=0; rewind(fp); printf("Numbers you entered : \n"); while (1) { if (fread(&numbers, sizeof(float), 1, fp) != 1) break; printf("%f ", numbers); average=average+numbers; i++; } average=average/i; system("pause"); rewind(fp); i=0; for (i=0; i<=100; i++) { if(fscanf(fp, "%f", &array[i])!=1) break; } for(j=0;j<i;j++) { printf("%f",array[j]); } system("pause"); }
Last edited by krakatao; 03-11-2012 at 11:08 AM.
- Get rid of ALL the system(pause) commands. Now. Before you do anything else, because you tend to forget and not change issues.
- array[] doesn't seem to be defined anywhere. Why did you delete it?
- Why are you scanning in the numbers twice, once with fread() and once with fscanf()? If I understand this correctly, there should be one loop getting numbers and writing them to the file, and one loop reading from the file and printing numbers. Why you have four loops is beyond me.
- Don't mix fscanf/fprintf API with fread/fwrite API. If you're trying to do formatted I/O, don't use the binary fwrite/fread.
- Get rid of the while(1) .... break syntax. It's usually a sign of less-planned code.
- The variables "k", "now", "temp", and "minimum" don't seem to be doing anything. Fix that or delete them.
- Why check for feof in a loop checking for feof? Not only is this practice non-standard for stdin (and bad practice in general, read this > FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com ), but it's redundant.
Fix all of this ^^ BEFORE you post back.