Write to random access file from struct?

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;

=========================================
//Main vehicle structure declared here
=========================================
struct Vehicle
{
char make[15];
char model[15];
int year;
char idNum[9];
char color[11];
char type[13];
double initialCost;
double salesPrice;
}vehInfo;


===================================================
//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";


outFile.open("randomfile", ios::in | ios::out | ios::binary | ios::trunc);

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;


morestuff[6][0] = 0;
morestuff[7][0] = 0;
cout << buffer << "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ " << endl;
strcpy(thing3,buffer);



nextP = strtok(thing3, " ");
strcpy(morestuff[morecount], nextP);


morecount++;
nextP = strtok(NULL, " ");
while (nextP != NULL)
{
strcpy(morestuff[morecount], nextP);
morecount++;
nextP = strtok(NULL, " ");
}

for (i=0; i < morecount; i++){
cout << morestuff[i] << "\\\\\\\\\\\\\\\\\\\\\\\\\\\ " << endl;
}


============================================
//Copy values to vehicle strucure
============================================
strncpy(vehInfo.make, morestuff[0], 15);
strncpy(vehInfo.model, morestuff[1],15);
number = atoi(morestuff[2]);
vehInfo.year = number;
strncpy(vehInfo.idNum, morestuff[3],9);
strncpy(vehInfo.color, morestuff[4],10);
strncpy(vehInfo.type, morestuff[5],12);
vehInfo.initialCost = atof(morestuff[6]);
vehInfo.salesPrice = atof(morestuff[7]);



cout << "This is the stucture" << endl;
cout << vehInfo.make << " " << vehInfo.model << " " << vehInfo.year << " " << vehInfo.idNum << " " << vehInfo.color << " " << vehInfo.type;
cout << setprecision(16) << " " << vehInfo.initialCost <<" " << vehInfo.salesPrice << endl;

=========================================================
//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++);




errCode |= -(recordcount)


}





return (errCode);
}
=============================================================================

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.
Last edited on
Topic archived. No new replies allowed.