How to read struct with numbers and string from binary file in C

1 Answer

0 votes
#include <stdio.h>
  
struct worker
{
    char name[16];
    int age;
    float salary;
};
  
int main(int argc, char **argv) 
{ 
    FILE *f;
    struct worker w;
  
    f = fopen("d:\\data.bin", "rb");
    if (!f)
    {
        printf("Unable to open d:\\data.bin");
        return 1;
    }
    for (int i = 1; i <= 3; i++)
    {
        fread(&w, sizeof(struct worker), 1, f);
        printf("%s %d %.2f\n", w.name, w.age, w.salary);
    }
             
      
    fclose(f);
      
    return(0);
}
  
/*
 
run:
 
dan 34 33874.00
rebecca 42 31873.00
dorothy 37 35874.00

*/

 



answered Aug 6, 2015 by avibootz

Related questions

1 answer 217 views
2 answers 282 views
282 views asked Dec 30, 2020 by avibootz
1 answer 273 views
1 answer 179 views
179 views asked Dec 30, 2020 by avibootz
1 answer 233 views
1 answer 200 views
200 views asked Jul 24, 2015 by avibootz
...