This one seems to work :-)

by Michael Calkins (Login MCalkins)
ASM Forum

For some reason, I couldn't create the file with fstream, so I went back to the older code that used ofstream.

I inserted a line to seekg to the beginning of the file, because it looked like it would be at the end otherwise.

The use of read and write is explained in the previous post.

I've been using the information from, including the downloadable tutorial.

>I really appreciate all the help, if theres anything i can do for you please let me know.

No problem. It was fun, and I learned quite a bit in the process. :-)

P.S. I was thinking that you could use a for loop instead of working with each of the 5 Items individually, by using pointer arithmetic. But then, why not just use an array, and get rid or the struct all together? (In main, you could use a multidimensional array...)


// JohnOnyon_10036178_Coursework1_C++.cpp : Defines the entry point for the console application.

// NOTE : This code has not been designed with re-use in mind. It has been coded to
// perform a specific task and in doing so may break OOP rules.

//#include "stdafx.h"
#include "iostream"
#include "iomanip"
#include "fstream"
#include "string"
#include "ctime"

using namespace std;

//Prototype function declerations
ifstream& OpenRecords(const char* FilePntr);

//This struct defines how the records file is both built and read.
//4 SalesRecord structures must exist for a complete record section.
//Each SalesRecord is an individual employees sales for that day.
struct SalesRecord
{int Item1, Item2, Item3, Item4, Item5;};

int main ()
// Title
cout << " Employee sales records.\n" << endl;

// Get the file stream
ifstream& fileStream = OpenRecords("SalesRecords.txt");

// Count the number of entries, create array to match and load the data.
// This is done by seeking for the end if file flag and dividing the pointers
// position by the size of the SalesRecord structure.
int NumberOfRecords = fileStream.tellg () / sizeof(SalesRecord);
cout << "\n " << NumberOfRecords << " records in file."<< endl;

// I think we need to go back to the beginning.

// Create array of SalesRecord struct and store the data in it.
SalesRecord* SalesRecs = new SalesRecord[NumberOfRecords]();
char * buffer;

cout << "\n\n Loading records, please wait..." << endl;
for(int i = 0; i < NumberOfRecords; i++)
// this takes the SalesRecs pointer, applies arithmetic to it to select a subscript, and then casts it as a char pointer.
buffer = (char *) (SalesRecs + i);, sizeof(SalesRecord));
// Debug line
cout << SalesRecs[i].Item1 << "\t" << SalesRecs[i].Item2 << "\t" << SalesRecs[i].Item3 << "\t" << SalesRecs[i].Item4 << "\t" << SalesRecs[i].Item5 << endl;
// Allow the user to see the final screen before exit.
delete & fileStream;
delete [] SalesRecs;
return 0;

// This function checks for the records files existance, if it exists, returns a pointer to the
// open file stream, If it does not exist then the program creates a dummy records file and
// restarts.
ifstream& OpenRecords(const char* FilePntr)
ifstream* InFileStream;
// Make sure the records file exists before running the program
cout << "\n Welcome, performing record check..." << endl;
// Attempt to open the file, if good return pointer of open file stream else report error and exit.
InFileStream = new ifstream(FilePntr,ios_base::binary);
if (InFileStream->good())
cout << "\n Record file found."<<endl;
else // If file not found create a dummy records file and restart the loading.
cout << "\n RECORD FILE NOT FOUND!\nCreating dummy data file...\n"<<endl;

//Delete the old filestream
delete InFileStream;
ofstream* OutFileStream;
SalesRecord sRec;
// this takes the address of sRec and casts it as a char pointer.
char * buffer = (char *) (& sRec);

//make a new filestream for output (ofstream)
OutFileStream = new ofstream(FilePntr,ios_base::binary);

// Seed random number generator

// Set limits for false sales data
int MaxSales = 10, MinSales = 0;

// Create a sales record structure to use to write the file.
for (int TotalRec = 0; TotalRec <= 29; TotalRec++)
// create 4 records per section (4 employees, one for each)
for (int RecCnt = 0; RecCnt <= 3; RecCnt++)
sRec.Item1 = (rand() % MaxSales) + MinSales;
sRec.Item2 = (rand() % MaxSales) + MinSales;
sRec.Item3 = (rand() % MaxSales) + MinSales;
sRec.Item4 = (rand() % MaxSales) + MinSales;
sRec.Item5 = (rand() % MaxSales) + MinSales;

//test pattern
sRec.Item1 = TotalRec;
sRec.Item2 = RecCnt;
sRec.Item3 = 1;
sRec.Item4 = 0;
sRec.Item5 = 0;

// Dump the random numbers to the file.
OutFileStream->write(buffer, sizeof(SalesRecord));
//Debug line - Remove from final
//*CheckFileStream << " : " << TotalRec << RecCnt << " : ";
//Delete the old filestream
delete OutFileStream;
//make a new filestream for input (ifstream)
InFileStream = new ifstream(FilePntr,ios_base::binary);
return *InFileStream;

This message has been edited by MCalkins on May 2, 2011 8:21 AM

Posted on May 2, 2011, 8:09 AM

Respond to this message   

Return to Index

Response TitleAuthor and Date
You the man MC!Unseen Machine on May 2
 *ywMichael Calkins on May 2
Here's my re-writeUnseen on May 2
 2nd Version -Unseen on May 2
  Nearly there...Unseen on May 2
   Re: Nearly there...Michael Calkins on May 2
    Thanks again MCUnseen on May 2
     arrays... (edited to fix post)Michael Calkins on May 3
      Multidimensional arrays and expandable arrays in C++ChronoKitsune on May 3

 Copyright © 1999-2018 Network54. All rights reserved.   Terms of Use   Privacy Statement