Jump to content
  • Advertisement
Sign in to follow this  

back to the File I/O grind

This topic is 4747 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings all. A while back some of you may have noticed my thread on a file i/o program that writes an fdf from a specified text document. Well... they liked it so much they wanted me to make it better... either that or it didn't work. One of the two ;) Last time was a pretty bad hack job of coding. So I'm trying to get a better design this time... esp. in case I have to change it again. Here's what I have so far
/*******************************************************************************
* File:        Record1099.h                     C++ Header File                *
* Author:      David Culpepper                                                 *
* Created:     05/23/05                                                        *
*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Description>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*
* This header file contains the declaration for a Record1099 struct that holds *
* all relevant information for a 1099.                                         *
*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Revision History>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*
* 05/23/05  -  Created                                                         *
*******************************************************************************/

#ifndef RECORD1099_H
#define RECORD1099_H

#include <string>
#include <iostream>

struct Record1099
{
   std::string filerName;
   std::string filerAddress;
   std::string filerCityStateZip;
   std::string filerPhone;
   std::string filerID;
   std::string sellerID;
   std::string sellerName;
   std::string sellerCityStateZip;
   std::string closingDate;
   std::string grossProceeds;
   std::string propertyAddress;
   std::string propertyCityStateZip;
   int recordNumber;
   
   void printToFDF(ostream &os);
   static int numRecords(0);
   static const string header("<< /V (");
   static const string trailer(")/T (Edit");
   static const string footer(")>>\n");
};// End Record1099

#endif


/*******************************************************************************
* File:        Record1099.cpp                   C++ Source File                *
* Author:      David Culpepper                                                 *
* Created:     05/23/05                                                        *
*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Description>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*
* This source file contains the definition for a Record1099 struct's functions.*
*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Revision History>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*
* 05/23/05  -  Created                                                         *
*******************************************************************************/

#include "Record1099.h"

#include <iostream>
#include <string>

using namespace std;

void Record1099::printToFDF(ostream &os)
{
   os << header << filerName << "\\r";
   os << filerAddress << "\\r";
   os << filerCityStateZip"\\r";
   os << filerPhone << trailer << recordNumber%3 + 1 << "a" << footer;

   os << filerID << trailer << recordNumber%3 + 1 << "b" << footer;
   os << sellerName << trailer << recordNumber%3 + 1 << "c" << footer;
   os << sellerID << trailer << recordNumber%3 + 1 << "d" << footer;
   os << sellerAddress << trailer << recordNumber%3 + 1 << "e" << footer;
   os << sellerCityStateZip << trailer << recordNumber%3 + 1 << "f" << footer;
   os << closingDate << trailer << recordNumber%3 + 1 << "g" << footer;
   os << grossProceeds << trailer << recordNumber%3 + 1 << "h" << footer;
   os << propertyAddress << trailer << recordNumber%3 + 1 << "i" << footer;
}




Two different files in case you didn't figure that out. This is really basic level I guess. I have no clue if it will compile or not. I know there are still some things I need to take care of like incrementing the recordNumber and stuff like that. But does this look better... esp to anyone that helped with the last one. My plan of attack is basically: 1. Open text file. 2. Change that into newline delimited data 3. Read that into a vector of records. 4. Write the vector into successive files Edit: Ok... what noob mistake am i making... It's not registering void printToFDF as a function... using dev cpp Edit2: Found my noob error [Edited by - NightStalker on May 23, 2005 1:40:19 PM]

Share this post


Link to post
Share on other sites
Advertisement
Overloading the (non-member) std::ostream& operator<<(std::ostream& os, const Record1099& rec); may be more convenient than having a printToFDF member function. If anything, it brings you in line with the rest of the C++ IOStream framework.

I don't think there is a need to store the header and trailers in static member variables. You could have them be const char* in the printToFDF function itself. While you're at it, you might do something like const int nrec = recordNumber%3 + 1;, that would save a bit of repetition.

As you can see, these are rather minor changes, which means I pretty much agree with your current approach. [smile]

Share this post


Link to post
Share on other sites
Okay... updated code... I took fruny's advice for most of the minor changes...
had actually been thinking about overloading the operator anyways.


/*******************************************************************************
* File: Record1099.h C++ Header File *
* Author: David Culpepper *
* Created: 05/23/05 *
*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Description>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*
* This header file contains the declaration for a Record1099 struct that holds *
* all relevant information for a 1099. *
*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Revision History>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*
* 05/23/05 - Created *
*******************************************************************************/


#ifndef RECORD1099_H
#define RECORD1099_H

#include <string>
#include <iostream>

const std::string header("<< /V (");
const std::string trailer(")/T (Edit");
const std::string footer(")>>\n");

struct Record1099
{

std::string filerName;
std::string filerAddress;
std::string filerCityStateZip;
std::string filerPhone;
std::string filerID;
std::string sellerID;
std::string sellerName;
std::string sellerAddress;
std::string sellerCityStateZip;
std::string closingDate;
std::string grossProceeds;
std::string propertyAddress;
std::string propertyCityStateZip;

Record1099() {recordNumber = ++numRecords;}
~Record1099() {numRecords--;}
int recordNumber;
static int numRecords = 0;
};// End Record1099

std::ostream& operator<<(std::ostream &os, const Record1099 &rec);

#endif

/*******************************************************************************
* File: Record1099.cpp C++ Source File *
* Author: David Culpepper *
* Created: 05/23/05 *
*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Description>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*
* This source file contains the definition for a Record1099 struct's functions.*
*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Revision History>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*
* 05/23/05 - Created *
*******************************************************************************/


#include "Record1099.h"

#include <iostream>
#include <string>

using namespace std;

ostream& operator<<(ostream &os, const Record1099 &rec)
{
const string header("<< /V (");
const string trailer(")/T (Edit");
const string footer(")>>\n");
const int nRec = rec.recordNumber % 3 + 1;

os << header << rec.filerName << "\\r";
os << header << rec.filerAddress << "\\r";
os << header << rec.filerCityStateZip << "\\r";
os << header << rec.filerPhone << trailer << nRec << "a" << footer;

os << header << rec.filerID << trailer << nRec << "b" << footer;
os << header << rec.sellerName << trailer << nRec << "c" << footer;
os << header << rec.sellerID << trailer << nRec << "d" << footer;
os << header << rec.sellerAddress << trailer << nRec << "e" << footer;
os << header << rec.sellerCityStateZip << trailer << nRec << "f" << footer;
os << header << rec.closingDate << trailer << nRec << "g" << footer;
os << header << rec.grossProceeds << trailer << nRec << "h" << footer;
os << header << rec.propertyAddress << trailer << nRec << "i" << footer;

return os;
}





Also, how would I go about setting up the incrementor for the number of class items I have. dev cpp isn't liking my static int numRecords = 0; Suggestions and code critique is welcome.

Share this post


Link to post
Share on other sites
And I don't like this too much.

    Record1099() {recordNumber = ++numRecords;}
~Record1099() {numRecords--;}


There are so many things that can go wrong. For example, if you pass a Record1099 by value to a function, numRecord doesn't get incremented (nor does recordNumber obviously), but it will get decremented when the function returns and the copy is destroyed.

You may be better off keeping an external counter and incrementing it manually (e.g. when you're reading the records the first time). Something like records.push_back( Record1099( numRecords++ ) ); isn't too far-fetched (though I grant you it might look better if you split that into three separate statements).

Regarding your static int numRecords = 0;, you will need to put static int numRecords; in the header file to declare the variable, and int Record1099::numRecords = 0; in a source file to define it.

I don't see why you're defining multiple header, trailer and footer variables (one set in the header, one set in the function). And as I mentioned earlier, they're prime candidates for getting stuffed into simple const char* const, though that's a minor issue (no need to build three string objects every time you call the function when it could just refer to a bunch of string literals.)

Share this post


Link to post
Share on other sites
I see what you mean. And after very little deliberation, I decided I don't want it to decrement anyways. For example if a middle record(one created somewhere between the first and most recent) is deleted then the next one i create will have the same number as the last record... hence two records with the same number... not good. Well it's just about lunch time for me... So i'm gonna take a break. Thanks again for your help fruny. Bear with me on the next parts. This is my first adventure into the standard c++ library.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!