-1

Need to loop through the fread statements and print each one. However feof is printing an extra line at the end

The code works (as in is getting the right output) just need to not have that extra line

a a a a a a a a a a a a a a a a a

a

struct item input;

 FILE *fptr;
 fptr = fopen(argv[1], "rb");
if(!fptr){
  FILE *fpOut = fopen(argv[1], "w");
 int c;
}

 if(fptr == NULL){
  fprintf(stderr, "\nError opening file\n");
  exit(1);

 fseek(fptr, 0, SEEK_SET);
}
while(!feof(fptr)){


 fread(&input.business, sizeof(float), 1, fptr);
 fread(&input.jellyfish, sizeof(char), 1, fptr);
 fread(&input.death, sizeof(input.death), 1, fptr);
 fread(&input.love, sizeof(input.love),1,fptr);
 fread(&input.ornament, sizeof(input.ornament), 1, fptr);
 fread(&input.taste, sizeof(input.taste),1,fptr);
 fread(&input.cloth, sizeof(input.cloth),1,fptr);
 fread(&input.name, sizeof(input.name),1,fptr);
 fread(&input.camera, sizeof(input.camera),1,fptr);
 fread(&input.attraction, sizeof(input.attraction),1,fptr);
 fread(&input.bottle, sizeof(input.bottle),1,fptr);
 fread(&input.stage, sizeof(input.stage),1,fptr);
 fread(&input.square, sizeof(input.square),1,fptr);
 fread(&input.bushes, sizeof(input.bushes),1,fptr);
 fread(&input.heat, sizeof(input.heat),1,fptr);
 fread(&input.fly, sizeof(input.fly),1,fptr);
 printf("%f ", input.business);
 printf("%c ", input.jellyfish);
 printf("%d ", input.death);
 printf("%c, ", input.love);
 printf("%d, ", input.ornament);
 printf("%f, ", input.taste);
 printf("%ld, ", input.cloth);
 printf("%d, ", input.name);
 printf("%d, ", input.camera);
 printf("%d, ", input.attraction);
 printf("%d, ", input.bottle);
 printf("%u, ", input.stage);
 printf("%f, ", input.square);
 printf("%d, ", input.bushes);
 printf("%s, ", input.heat);`
 printf("%d \n", input.fly);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Hugh
  • 9
  • 1

1 Answers1

0

The check of eof() occurs after fread.

while(!feof(fptr)){
    fread(&input.business, sizeof(float), 1, fptr);
    //...
    printf("%f ", input.business);
    //... 
}

So a call of fread can end unsuccessfully and the previous value of the corresponding argument will be outputted by a call of printf.

From the C Standard (7.21.8.1 The fread function)

3 The fread function returns the number of elements successfully read, which may be less than nmemb if a read error or end-of-file is encountered. If size or nmemb is zero, fread returns zero and the contents of the array and the state of the stream remain unchanged

I can suggest the following approach to your task.

enum { N = 16 }; // number of fread calls
size_t n = 0, prev_n = 0;

do
{
    switch ( n )
    {
    case 0:
        n += fread( &input.business, sizeof(float), 1, fptr );
        break;
    case 1:     
        n += fread(&input.jellyfish, sizeof(char), 1, fptr );
        break;
    case 2:
        n += fread(&input.death, sizeof(input.death), 1, fptr );
        break;
    // ...
    }
} while ( prev_n != n && ( prev_n = n ) < N );

for ( size_t i = 0; i < n; i++ )
{
    switch ( i )
    {
    case 0:         
        printf("%f ", input.business);
        break;
    case 1:         
        printf("%c ", input.jellyfish);
        break;
    case 2:         
        printf("%d ", input.death);
        break;
    //...           
    }
}       

Here is a demonstrative program that shows the approach in action.

#include <stdio.h>

int main(void) 
{
    enum { N = 3 };
    int a[N];
    size_t n = 0, prev_n = 0;

    do
    {
        switch ( n )
        {
        case 0:
            n += scanf( "%d", a + n );
            break;
        case 1:     
            n += scanf( "%d", a + n );
            break;
        case 2:
            n += scanf( "%d", a + n );
            break;
        }
        // ...
    } while ( prev_n != n && ( prev_n = n ) < N );

    for ( size_t i = 0; i < n; i++ )
    {
        switch ( i )
        {
        case 0:         
            printf( "%d ", a[i] );
            break;
        case 1:         
            printf( "%d ", a[i] );
            break;
        case 2:         
            printf( "%d ", a[i] );
            break;
        //...           
        }
    }       

    return 0;
}

if the input is

1 2 3

then the output is also

1 2 3
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335