Hi, I am having some trouble writing the contents of a structure to a random access file. I am supposed to parse the string, copy it to a structure and write it out to a file but when I try to do this my output file always appears empty.
This is a vehicle inventory program by the way.
==============================================================================
//heres main
==============================================================================
#include <cstring>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
const int ERR_MAKE = 0x1;
const int ERR_MODEL = 0x2;
const int ERR_YEAR = 0x4;
const int ERR_ID = 0x8;
const int ERR_ID_TAKEN = 0x10;
const int ERR_COLOR = 0x20;
const int ERR_STYLE = 0x40;
const int ERR_COST = 0x80;
const int ERR_PRICE = 0x100;
const int ERR_LOW_PRICE = 0x200;
const int ERR_DATABASE_FULL = 0x20000;
const int ERR_TARGET_PRICE = 0x4000;
const int ERR_ARRAY_TOO_SMALL = 0x8000;
const int OPEN_INFILE_FAIL = 0x40000;
const int INVALID_CMD = 0x80000;
const int MaxLineChars = 300;
const int MaxWordChars = 20;
const int MaxLines = 100;
using namespace std;
fstream outFile;
ifstream inFile;
===================================================
//Secondary structure to store id and record number
====================================================
struct Record
{
int idNum;
int recordNum;
}vehIndex[100];
=================================
//main()
=================================
int
main0()
{
int retCode=0;
initService("randomfile.txt");
retCode = processData("batch.txt");
cin.ignore();
cin.get();
return(retCode);
}
=======================================================
//initservice recieves random file and opens for output
=======================================================
int initService(const char *randomfile){
int errCode = 0;
int recordcount = 0;
cout << "THIS IS A VEHICLE INVENTORY PROGRAM\n\n";
cout << "The following batch commands with the specified syntax are accepted:" << endl;
cout << "Add Manufacturer Model Year ID Color Style Cost ListPrice\n"
<< "Matches Color Style Price\n"
<< "Sell ID OfferPrice\n"
<< "Display ID\n"
<< "DisplayAll\n"
<< "Exit";
if (outFile.fail()){
cout << "RANDOM FILE FAILED TO OPEN |||||||||||||||||||| " << endl;
errCode |= -1;
}
return (errCode);
}
=====================================================================
//addVehicle function parses string,copies to structure and writes
out to random access file
=====================================================================
int addVehicle (char const buffer[MaxLineChars+1]){
char morestuff[8][MaxWordChars+1];
int morecount = 0;
int number;
int retCode = 0;
double cost, price;
char *nextP;
int i;
char thing2[MaxWordChars+1];
char thing3[MaxWordChars+1];
int j = 0;
int val = 0;
int val1 = 0;
long int val2;
long int val3;
long int val4;
int val5;
int val6;
int val7;
double val8;
int val9;
double val10;
int errCode = 0;
static int recordcount = 0;
int idNum;
int AddVehCount;
bool error = false;
bool bufferError = false;
=========================================================
//This is where I try to write to the file
========================================================
outFile.seekp(recordcount * sizeof(Vehicle), ios::beg);
outFile.write(reinterpret_cast<char*> (&vehInfo), sizeof(Vehicle));
cout << "Vehicle added successfully " << endl;
=========================================================
This just adds the id and recordnum from vehInfo struct to
vehIndex struct
=========================================================
idNum = atoi(vehInfo.idNum);
vehIndex[recordcount].idNum = idNum;
cout << vehIndex[recordcount].idNum << endl;
vehIndex[recordcount].recordNum = (recordcount++);
There are no compiler errors but when I check 'randomfile' its always empty...any suggestions are much appreciated.NOTE: the addvehicle function is in the processData function.I didn't want to post it because it would be too much code.