I am having trouble bring information from a file into a structure and printing it out on the screen. How is that done? Here is the code I have that has the structure and the open statement. But not sure how to pull the data from the file and print it out.. Is that a plain printf statement?
Code:
#include <stdio.h>
#include <stdlib.h>
int num_comp; /*place holder for number of companies */
float num_months; /*place holder for number of months */
struct company{ /*structure to hold the file of company.txt*/
char city[30];
float months[10];
char name[20];
char manager[20];
};
struct company *comp; /*create structure pointer */
int main(int argc, char **argv){
FILE *fp;
char filename[20];
int choice, sum, counter;
double average;
float month;
comp = (struct company *)malloc(sizeof(struct company));
printf("Please enter the name of the relevant file: ");
scanf("%s", &filename);
fp = fopen("filename", "r"); /*to open a current file*/
fscanf(fp, "%s", &filename);
printf("1. Enter 1 to get the statistics for a specific month.\n");
printf("2. Enter 2 to get the overall statistics.\n");
printf("3. Enter 3 to terminate the program.\n");
printf("Please enter your choice:\n" );
scanf("%d", &choice);
switch(choice){
case 1:
printf("Which month are you interested in?");
scanf("%f", &month);
if (month > 4)
printf("Sorry, this data is not available, please enter a valid integer from 1 to 4\n");
else
for (counter =1; counter <= 19; counter=counter++)
{
average = sum/counter;
}
printf("Average sale for month %d: %f \n", month, average);
break;
case 2:
case 2:
printf("The statistics are below: %s\n ", filename);
break;
case 3:
printf("Good Bye!\n");
break;
default:
break;
fclose(fp);
}
return 0;
}