Hi davic01!
Here an example:
first student.h:
Code:
#ifndef STUDENT_INC_
#define STUDENT_INC_
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <didi.h>
// Setze Vordergrundfarbe \x1b ist ESC 27
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define BLUE "\x1b[34m"
#define MAGENTA "\x1b[35m"
#define CYAN "\x1b[36m"
#define WHITE_GREEN "\x1b[1;32;47m" // Backround = White, Foreground = Green --> Vordergrund;Hintergrund
#define BLUE_YELLOW "\x1b[1;34;43m"
#define WHITE_BLACK "\x1b[1;37;40m"
#define BLACK_YELLOW "\x1b[1;30;43m"
#define BLACK_WHITE "\x1b[1;30;47m"
#define BLUE_WHITE "\x1b[1;34;47m"
#define BLUE_GREEN "\x1b[1;34;42m"
#define BLACK_GREEN "\x1b[1;30;42m"
#define COLOR_RESET "\x1b[0m"
int initStructs(char filename[], struct StudentData (*Students)[5]);
void printStudentData(struct StudentData (*Students)[5], int reccount);
#endif
now student.cpp
Code:
#include "student.h"
int initStructs(char filename[], struct StudentData (*Students)[5])
{
FILE * input; //create file pointer
int pos,ELA,Math,Science = 0; // struct array pointer
char n[25] = {0}; // to retrieve student name
double ID; // to retrieve student ID
int rewer = -1;
input = fopen(filename, "r");
if(!input)
{
std::cout << "Unable to open file" << std::endl; //output if file could not be opened
return rewer;
}
pos = 0;
while (fscanf(input,"%lf %s %d %d %d", &ID, n, &ELA, &Math, &Science) != EOF)
{
//Read the data from each line into struct
strcpy(Students[pos]->name, n);
Students[pos]->ID = ID;
Students[pos]->ELA = ELA;
Students[pos]->Math = Math;
Students[pos]->Science = Science;
//printf("pos=%d %lf %s %d %d %d\n",pos, ID, n, ELA, Math, Science);
//printf("pos=%d %lf %s %d %d %d\n",pos, Students[pos]->ID, Students[pos]->name, Students[pos]->ELA, Students[pos]->Math, Students[pos]->Science);
pos++;
}
//for(int i=0; i< pos; i++) printf("nr=%d %8.2lf\t %-20s\t%d\t%d\t%d\n", i, Students[i]->ID, Students[i]->name, Students[i]->ELA,Students[i]->Math,Students[i]->Science);
std::cout << "Finished reading from file:" << std::endl << filename << std::endl;
std::cout << "reccount: " << pos << std::endl;
fclose(input); //close the file
return pos;
}//end initStructs
/* printStudentData is a function that loops through the Students array
* prints the data in each student struct to screen
*/
void printStudentData(struct StudentData (*Students)[5], int reccount)
{
std::cout << "\n\nPrinting " << reccount << " Records from file..." << std::endl << std::endl;
std::cout << " ID\t\t Name\t\t\tELA\tMath\tScience" << std::endl;
std::cout << MAGENTA << "-----------------------------------------------------------------" << COLOR_RESET<< std::endl;
for(int i=0; i< reccount; i++)
{
std::cout << BLUE << std::left << std::setw(10) << Students[i]->ID;
std::cout << WHITE_BLACK << "\t" << std::left << std::setw(20) << Students[i]->name << "\t";
std::cout << CYAN << Students[i]->ELA << "\t" << WHITE_BLACK << Students[i]->Math << BLUE_WHITE << "\t" << Students[i]->Science << COLOR_RESET << std::endl;
}
std::cout << RED << "-----------------------------------------------------------------" << COLOR_RESET << std::endl;
std::cout << std::endl<< "Ende der Liste" << std::endl;
}
main.cpp:
Code:
#include "student.h"
int main(int argc, char **argv)
{
struct StudentData Students[5]; //array to hold 5 StudentData structures
char finam[200] = {0};
int reccount = 0; //keeps count of number of records read in from file
strcpy(finam, "test.txt");
reccount = initStructs(finam, &Students);
printStudentData(&Students, reccount);
return EXIT_SUCCESS;
}
here test.txt
Code:
1001 Janet 81 42 65
1002 John 47 68 72
1003 Jacob 92 94 87
1004 Jill 96 88 89
1005 Jack 54 62 41
example output:
May be some guys think the colours are awful, so change it!