Hey there,

I'm writing my final lab project for my programming class and I'm running into a problem.
I need to read in employee information line by line from an existing file. The file opens and reads info in, but the distribution of the info on the line is staggered through the fields, and random numbers and letters show up on some lines while others are left blank. I'm getting desperate because my teacher even give us a hint, and our textbook doesn't explain how you are supposed to read in line by line from a file.
Any help would be really appreciated.

Thanks!
Emily

Code:
#include <stdio.h>
#include <string.h>
#define MAX 7

typedef struct
{
	char name [30];
	char title [30];
	double salary;
	int years;
}EMPLOYEE;


void print_back(EMPLOYEE values[], int number_employees);
void calculate_salary(EMPLOYEE values[], int number_employees);
void get_retirees(EMPLOYEE values[], int number_employees);

int main()
{
	int option, foo;
	char filename [15];
	
	printf("\nPlease select an option from the menu below.");
	printf("\n********************************************");
	printf("\n1. Read employee information from an existing file");
	printf("\n2. Write employee information to a new or existing file");
	printf("\n\nPlease enter your selection: ");
	scanf("%d", &option);
	
	if (option == 1) /*read*/
	{
		printf("\nPlease enter the name of the file (format: myfile.txt): ");
			foo = scanf("%s", filename);
			if (foo != 0)
			{
				FILE *input_file;
				int i;
				
				input_file = fopen(filename,"r"); /*open*/
				
				if (input_file != NULL)
				{
					int i=0, count;
					EMPLOYEE employees [MAX];
					int number_employees=0;	
								
					while (fgets(employees[i].name, 30, input_file) != NULL)
					{
						i++;
						fgets(employees[i].title, 30, input_file);
						fscanf(input_file, "%lf", &employees[i].salary);
						fscanf(input_file, "%d", &employees[i].years); /*buffer thing*/
						number_employees++;			
					}
				
					print_back(employees, number_employees);
					calculate_salary(employees,number_employees);
					get_retirees(employees, number_employees);
				}
				else
				{
					printf("\nFile failed to open, quitting!");
				}
				
				
			}
	}
	else if (option == 2) /*write*/
	{
		FILE *output_file;
		int number_employees, i;
		char lastname [15];
	
		printf("\nThank you.");
		printf("\nPlease enter the name of the file to write to (format myfile.txt): ");
		scanf("%s", filename); /*information entered stored in str "filename"*/
		printf("\nHow many employees will you be entering: ");
		scanf("%d", &number_employees);
		
		EMPLOYEE employees [number_employees];
		
		output_file = fopen(filename, "a"); /*open file*/
		
		
		printf("\nFile successfully opened.");
		printf("\nPlease enter employee information.");
		
		for(i=0; i < number_employees; i++) /*get info from user to print to "filename"*/
		{
		printf("\nEmployee Name: ");
		scanf("%s%s", employees[i].name, lastname);
		strcat(employees[i].name, " ");
		strcat(employees[i].name, lastname);
		fprintf(output_file, "%s\n", employees[i].name);
		printf("\nEmployee Title: "); /*possibility of multiple word titles, later...*/
		scanf("%s", employees[i].title);
		fprintf(output_file, "%s\n", employees[i].title);
		printf("\nSalary: ");
		scanf("%lf", &employees[i].salary);
		fprintf(output_file, "%0.2f\n", employees[i].salary);
		printf("\nYears Employed: ");
		scanf("%d", &employees[i].years);
		fprintf(output_file, "%d\n", employees[i].years);
		}
		
		fclose(output_file); /*close file*/
	
		printf("\nFile successfully written.  Retrieve entered data from file %s.", filename);
	}
	else
	{
		printf("\nInvalid selection, quitting!\n");
	}
	
	printf("\nThank you for employee-fying.  Have a nice day.\n");
	
	return(0);
}
/*print back employee information*/
void print_back(EMPLOYEE values[], int number_employees)
{
        int i;

        printf("\nEmployee Information");
        printf("\n********************\n");
        printf("\nEmployees entered: %d\n", number_employees);

        for (i=0; i < number_employees; i++)
        {
                printf("\nName: %s", values[i].name);
                printf("\nTitle: %s", values[i].title);
                printf("\nSalary: $%0.2lf", values[i].salary);
                printf("\nYears Employed: %d\n", values[i].years);
        }
}
/*this function performs all salary information*/
void calculate_salary(EMPLOYEE values[], int number_employees)\
{
        int i;
        double total=0, average=0;

        for (i=0; i < number_employees; i++)
        {
                total += values[i].salary;
        }

        average = total / number_employees;

        printf("\nYearly salary expense: $%0.2f", total);
        printf("\nAverage of employee salaries: $%0.2f", average);
}
void get_retirees(EMPLOYEE values[], int number_employees)
{
        int i, retirees=0;

        printf("\n\nEmployees near retirement:");

        for (i=0; i < number_employees; i++)
        {
                if (values[i].years >= 15)
                {
                        printf("\n%s", values[i].name);
			retirees++;
                }
        }

	printf("\nNumber of employees near retirement: %d", retirees);
}