3 Replies - 1005 Views - Last Post: 23 April 2011 - 07:28 PM Rate Topic: -----

#1 xamo18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-April 11

Write from struct to random access file?

Posted 23 April 2011 - 04:48 PM

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.txt", 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.

This post has been edited by xamo18: 23 April 2011 - 04:50 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Write from struct to random access file?

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Write from struct to random access file?

Posted 23 April 2011 - 05:05 PM

Troubleshoot it step by step:

1) Are you getting the data, into your structs, correctly - no errors at all?

2) Are you able to print the data correctly, to the screen, without errors?

3) Are you able to open the file OK? Can you write any other data to the file?

That step by step approach will lead you right to the problem in your code. Troubleshooting is an essential skill for any programmer to learn. Bugs are not going away anytime soon. ;)

This post has been edited by Adak: 23 April 2011 - 05:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 xamo18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-April 11

Re: Write from struct to random access file?

Posted 23 April 2011 - 05:27 PM

View PostAdak, on 23 April 2011 - 05:05 PM, said:

Troubleshoot it step by step:

1) Are you getting the data, into your structs, correctly - no errors at all?

2) Are you able to print the data correctly, to the screen, without errors?

3) Are you able to open the file OK? Can you write any other data to the file?

That step by step approach will lead you right to the problem in your code. Troubleshooting is an essential skill for any programmer to learn. Bugs are not going away anytime soon. ;)

I finally got it to write to my 'randfile' it turns out that ios::trunc kept deleting the contents of the output file.Don't know why my teacher insisted that we use it.One problem though, It writes to the random file but whats odd is that it keeps saying that the 'randomfile' failed to open when I clearly wrote all that data to it.


int initService(const char *randomfile){
    
 int errCode = 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.txt", ios::in | ios::out | ios::binary);
 if (outFile.fail()){
  cout << "FAILED TO OPEN FILE!!! " << endl;  
  errCode |= -1;
}

 
 
 
 return (errCode);
}




Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Write from struct to random access file?

Posted 23 April 2011 - 07:28 PM

I program in C, not C++, so I can't help you with open.fail.

Your IDE should have some info on it (or man pages if you are on Linux). There is also a C++ site (maybe called Cplusplus?) that has a lot of info on C++ topics and common usage.

Give that a google, and well done. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1