#include <stdio.h>
#include <stdlib.h>
int main()
{
int count=0;
float data, compare = -9999.0;
FILE *fptr1, *fptr2;
if((fptr1 = fopen("hello.dat", "r")) == NULL)
{
printf("Error opening file\n");
exit(1);
}
if((fptr2 = fopen("hello1.dat", "w")) == NULL)
{
printf("Error opening file1\n");
exit(1);
}
else
{
while( !feof(fptr1) )
{
fread(&data, sizeof(float), 1, fptr1);
if(data > compare)
compare = data;
fwrite(&data, sizeof(float), 1, fptr2);
count++;
}
fprintf(fptr2, "\n\n%s", "Max float number = ");
fprintf(fptr2, "%d\n\n", compare);
fprintf(fptr2, "%s", "No of float values = ");
fprintf(fptr2, "%d\n", count);
fclose(fptr1);
fclose(fptr2);
}
return 0;
}
Here are my problems:
I don't know why when 'hello1.dat' was created, the last value of 'hello.dat' was repeated twice? I have ensured that no new line was entered at the last value when creating this 'hello.dat' file.
I also don't know why it didn't print out the 'compare' value?
hello.dat is just number of values of float.