It outputs the data correctly into the .txt file. But when reading it. It doesn't come out right.
Code:#include <stdio.h> #include <stdlib.h> #include <time.h> class Data { public: void Output(char*, int); void Input(int); Data(); ~Data(); private: FILE *out; FILE *in; int *Age; int rand_age; }dt; Data:: Data() { srand((int)time(NULL)); out= fopen("Stats.txt", "w"); in= fopen("Stats.txt", "r"); Age= new int[2]; rand_age=1 + (rand() % 15) + 10; } void Data::Output(char *Name, int ID) { Age[ID]= rand_age; fprintf(out, "Name: %s\n", Name); fprintf(out, "Age: %i\n\n", Age[ID]); } void Data::Input(int ID) { char *Name; fscanf(in, "Name: %s\n", Name); fscanf(in, "Age: %d\n", Age[ID]); printf("\nName: %s", Name); printf("\nAge: %d\n", Age[ID]); } Data::~Data() { fclose(out); fclose(in); delete [] Age; } int main(int argc, char *argv[]) { if(sizeof(Data) > 0) { dt.Output("Jerome", 0); dt.Output("James", 1); dt.Input(0); dt.Input(1); } printf("\n\n"); system("pause"); return EXIT_SUCCESS; }



LinkBack URL
About LinkBacks


