I'm having an error with my code when executed about the variable 's' being corrupted. I believe it's something with saving the bin or the text. I've tried editing more with the text, but can't figure it out thinking it might be the bin or something I must have mistyped or added on accident.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 4
struct Person
{
char name[16], dep[16];
float cyi, ra, rp, npa, tyi, tra, tpa;
};
void load(struct Person s[], int n)
{
for (int i = 0; i < n; i++)
{
printf("Enter your name ");
gets(s[i].name);
printf("Enter your department ");
gets(s[i].dep);
printf("Enter your current yearly income $");
scanf("%f", &s[i].cyi);
printf("Enter your raise percentage ");
scanf("%f", &s[i].rp);
s[i].ra = (s[i].cyi * s[i].rp) / (float)100;
s[i].npa = (s[i].cyi) + (s[i].ra);
printf("\n");
fflush(stdin);
}
}
void sort(struct Person s[], int n)
{
int i, j;
Person t;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - 1; j++)
if (strcmp(s[j].name, s[j + 1].name) > 0)
{
t = s[j];
s[j] = s[j + 1];
s[j + 1] = t;
}
}
void print(struct Person s[], int n)
{
printf("\n\n");
for (int i = 0; i < n; i++)
{
printf("%s in department %s\n", s[i].name, s[i].dep);
printf("The current yearly income is $%0.2f the raise percentage is %0.2f%%\n", s[i].cyi, s[i].rp);
printf("The raise amount is $%0.2f, the new pay amount is $%0.2f\n\n", s[i].ra, s[i].npa);
}
}
void calc(struct Person s[], int n)
{
float tyi = 0, tra = 0, tpa = 0;
for (int i = 0; i < n; i++)
{
tyi += s[i].cyi;
tra += s[i].ra;
tpa += s[i].npa;
}
printf("The total current yearly income is $%0.2f\n", tyi);
printf("The total raise amount is $%0.2f\n", tra);
printf("The total new pay amount is $%0.2f\n", tpa);
}
void savetext(struct Person s[], int n)
{
int i;
FILE *f;
f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.txt", "w");
for (i = 0; i < n; i++)
{
fprintf(f, "%s\n", s[i].name);
fprintf(f, "%s\n", s[i].dep);
fprintf(f, "%f %f %f\n", s[i].cyi, s[i].rp, s[i].npa);
}
fclose(f);
}
void retrievetext(struct Person s[], int n)
{
int i;
FILE *f;
f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.txt", "r");
for (i = 0; i < n; i++);
{
fgets(s[i].name, sizeof(s[i].name), f);
fgets(s[i].dep, sizeof(s[i].dep), f);
fscanf(f, "%f%f%f\n", &s[i].cyi, &s[i].rp, &s[i].npa);
}
fclose(f);
}
void savebin(struct Person s[], int n)
{
FILE *f;
f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.bin", "wb");
fwrite(&s, sizeof(s[0]), n, f);
fclose(f);
}
void retrievebin(struct Person s[], int n)
{
FILE *f;
f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.bin", "rb");
fread(&s, sizeof(s[0]), n, f);
fclose(f);
}
void main()
{
Person s[SIZE];
load(s, SIZE);
sort(s, SIZE);
print(s, SIZE);
calc(s, SIZE);
savetext(s, SIZE);
retrievetext(s, SIZE);
printf("\nAfter the text file is retrieved\n");
print(s, SIZE);
savebin(s, SIZE);
retrievebin(s, SIZE);
printf("\nAfter the binary file is retrieved\n");
print(s, SIZE);
system("PAUSE");
}
This is for a homework assignment of mine for class.
Error Received is screenshotted here