|
In my assignment I am to construct a program that will read from a file. From this file, is the information such as student name, offense, #frequency of offense, and prior offenses. The program must take this into account and recommend a punishment. 10 students 5offenses.
The program should read its input form a text file using the fscanf function. Once the program reads in a line the data should be copied into a struct and processed.
The program is to read from the file until end-of-file is reached.
The program should read and process each record from the input file, on record at a time. There is no need to create an array of records. Use a while loop to drive the process. For each record, the program should consider the offense, the number of times it has been committed and the student's overall record. After considering these factors, the program should choose a sanction and print the name of the student on the screen and the recommended punishment.
My psuedo code is:
print & scan files into .txt
fscanf & copy into a struct.
then,
it should read from file until EOF
My question is, how do i get the info from the txt file into the struct?
CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct person
{
char name[50];
char offense[50];
double frequency[3];
double priors[4];
};
int main(void)
{
FILE *input;
struct person student[10];
int i = 0; // counter for the array index
int j = 0; // counter for display loop
input = fopen("input.txt","r");
while(feof(input)==0)
{
fscanf(input," %s ", student[i].name);
fscanf(input," %s ", student[i].offense);
fscanf(input," %s ", student[i].frequency);
fscanf(input," %s ", student[i].priors);
i++; // increment for next time through the loop
}
for (j = 0; j < i; j++) // remember that i has the number of entries in the array
{
printf("%s %s %s %s\n", student[j].name, student[j].offense, student[j].frequency, student[j].priors);
}
fclose(input);
getchar();
}
|