Pointer-based data loading from a (text) file

I have two classes, productListType and buyerListType, that are each basically linked lists. To create the linked list for an object of productListType, I successfully wrote a non-class function createProductList to read the product data from a text file. The class definition for buyerListType has a data member productBoughtRecord that is of type productListType to aggregate the details of the products purchased by a particular buyer during transactions. I decided to make productBoughtRecord a pointer since the contents of this data member would wax and wane over the course of several transactions, depending on the amount and frequency of payments made by the buyer. I have provided a rough sketch of the class below:

1
2
3
4
5
6
7
8
9
10
11
12
13
class buyerListType
{
public:
    setCustomerInfo( ....., productListType* p);
      .
      .
      .      

private:    
    productListType* productBoughtRecord;
      .
      .
};


I'm similarly trying to write a non-class function createBuyerList to load the record of customers from a text file. How do I determine what the value of the formal parameter p in member function setCustomerInfo is, in order to be able to set the data member productBoughtRecord? Please, take into consideration that an object of class buyerListType would have multiple buyers with varying amounts of products purchased.
Last edited on
Does anyone have an insight into my problem?
What my understanding is:
You have 2 data files for products and customers, and you have loaded the product list in to productListType list.
Question arises that is there any information of product(s) in customer file to identify any mapping between productListType and buyerListType.
@upX86:

Well yeah, you pretty much get the picture. Let me be more elaborate to give you a better grasp. The full declaration of that member function setCustomerInfo in my OP is:

void setCustomerInfo(string firstName, string lastName, int acctNo, productListType* p);

The general format for the customer text file is:

Customer's name
Customer's Account number
List of products bought


Class productListType has about six data members(i.e. productName, productPrice, productWeight, productManufacturer, product Date of purchase, productPaymentMade).

So the customer text file may look like this:

Henry Baker
2786493
Product #1 data
Product #2 data

Emily Blade
3728123
Product#1 data
Product#2 data
Product#3 data
Product#4 data

Lucy Foster
5003744
Product#1 data

.
.
.

As you can see above, customers(i.e. buyerListType objects) would have differing amounts of products bought. The main problem I'm having is: In the process of creating the linked list for a buyerListType object, how do I use formal parameter p of the setCustomerInfo function in class
buyerListType to load the list of product information from the text file into each customer object??? That is the crux of my problem!!!
Is Product #1 data the same as entry in product file?
Thx upX86! If I get your question correctly, yes. If Product#1 data for Henry Baker is about a lawn mower, then we can state that the data members for a lawn mower in the product file are the same as those for Henry. If Product#3 for Emily is a blender, then this is the same as the blender in the product file. Conversely, I could also say 'no' because Product#1 for Henry is not necessarily the first object loaded into the productListType object that I said I had successfully created (I would not think this is the sense you conveyed in your question). Moreover, Henry's Product#1 is very likely different from Emily Baker's Product#1, and different from Lucy Foster's.
Hi,
if i understood it correctly, you are storing the whole produkt information in the buyerList file?

I would advise you to give each produkt an id. Then after loading your two files, you could iterate through your product list for each id found in the buyerfile and search for the id in the product object ( simple way ).

As this would take some time, i would load the product objects in an array/vector with the id beeing the position. This would be much faster to access. For deleting and adding products you would need an object to manage the ids.
Yep that's clear to me that any product in customer's buy list would be from product file only or should say that product file is the inventory.

If it is so then in this scenario for maintaining the inventory linked list data structure is inappropriate. But it will work for buyer's cart.

So when you are reading the customer data file create a fresh productListType p for every customer and pass it in to HenryBaker.setCustomerInfo(...,p) In this condition you won't have to look at the productListType prepared by product file.
Maggistro wrote:
you are storing the whole produkt information in the buyerList
file?

No; I am storing the product info in the product text file. I do not have any
problems with loading the product file into a productListType object (which is essentially a linked list). However, as I've already explained, I am
trying to load the buyer file contents into a buyerListType object using the
setCustomerInfo() member function above. How do I use formal parameter p (which is a pointer) to access the product info part of the buyer text file (that I illustrated above) so that I could load this into the private data object, productBoughtRecord, of each buyerListType object???

upX86 wrote:
If it is so then in this scenario for maintaining the inventory
linked list data structure is inappropriate.

I have no choice, the problem requires me to use linked lists. Moreover, like I already stated, I have successfully been able to create a productListType linked list object; so, it being a linked list data structure is not the problem. But in that case, the class's member function that does the setting of the data members does not have a pointer formal parameter like class buyerListType's version.

If you guys look at my penultimate post, I gave actual examples of how the buyer text file data look like. What I am trying to do is read each buyer's data and insert that data into a buyerListType object. If you look at the member function below:

void setCustomerInfo(string firstName, string lastName, int acctNo, productListType* p);

Take the case of Henry Baker's data in that buyer/customer text file illustration above, you can see that I can easily read Henry into firstName, read Baker into lastName, read 2786493 into acctNo; now, how do I get the pointer that references Product#1 data and Product#2 data??? How can I access these product data, uniquely Henry Baker's, in this sense?
Last edited on
Ohkay...
If you are bound to use link list, do one thing when you do read operation from customer file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
buyerListType* ReadBuyerFromFile(file)
{
	buyerListType *buyer = new buyerListType();
	name = readName(file);
	accNo = readAccNo(file);
	// formal pointer for reading products for this buyer.
	productListType *cartHead;
	while(!IsCartEmpty)
	{
		//create productListType node with 
		//Product#n from reading the customer file
		productListType *p = readProduct();

		//add product to cartHead's list
#ifndef upX86
		createProductList (castHead, p);
#else
		if (cartHead == NULL)
			castHead = p;
		else
			castHead->next = p;
#endif
	}
	//Now cartHead should have all prducts bought by 
	//buyer named "*name".
	buyer.setCustomerInfo(name,accNo,cartHead);
	//return new buyer to add in buyer-List
	return buyer ;
}


Note: Macros are just for multiple ways of creating productList, you can use either your's or mine.

Now there is no use of product list/file as every customer will have thier own cart prepared from customer file and constraint of using only link list is also fulfilled.
geeloso will it work for you?

EDIT: Typos and little bit in Note message.
Last edited on
@upX86:
Thx for your insight and the code snippet/template you wrote; it's really appreciated. I like the idea of using a dedicated function to return a pointer to the product data info of each buyer/customer. What I'm inferring from your code (which I have pondered prior) is that a text file does not have any pointer information resident in it per se (i.e. one that points to different sections of the text file) that could just be read and used; rather the text data has to be read first into a dynamically allocated node, whose associated pointer could now be used. I was hoping that there was a way of just determining the pointer to a character, string, or block of data in the text file which I could then load into the much-referenced setCustomerInfo() member function. I'm guessing that was too ambitious on my part!

I want you to consider two scenarios:

1. A buyer in the buyer text file that has no products (i.e. whose cart is empty); I would expect the function you wrote above to return NULL. Right?

2. Considering that buyers in the text file very likely have differing numbers of products in their corresponding product lists (i.e. carts), I would think this presupposes that I have a way of detecting where a buyer's product list ends before the name of the next buyer in the text file appears. I know of two or more ways to do this, but I don't want to use any of them because of their apparent simplicity and/or inefficiency:
a. injecting a unique character (i.e. a flag) after each buyer's product list;
b. using the newline character twice (i.e. pressing the ENTER key twice) after each product list segment; or
c. just setting an upper limit on the probable number of products and using an iterative for statement to read all the products.

Any better approach in your view??
geeloso by productListType *p = readProduct(); I mean reading the customer file itself for Product#1-to-N one by one

as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
productListType *readProduct()
{
	productListType *product = NULL;
	string product_string = readline(file);
	//using new line char as saperation of customer entry will give empty string.
	//so your point 2.b will suffice
	if ( !product_string.empty() )
	{
		//here you have the raw product row/record from customer file.
		//Create new productListType data node for this product entry.
		product = new productListType();
		
		//Extract product info from string as you would have been doing for product file
		product->setProductInfo(product_string);
	}
	return product;	
}


For point 1. bit addition in earlier code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
buyerListType* ReadBuyerFromFile(file)
{
	buyerListType *buyer = NULL;
	string str = readline(file);
	bool IsCartEmpty = false;//it does mean empty in file.
	
	//If there is an entry it should mean it is buyer's info.
	//if it is not present it will return a NULL pointer of buyerListType
	if ( !str.empty() )
	{
		buyer = new buyerListType();
		name = readName(file);
		accNo = readAccNo(file);
		// formal pointer for reading products for this buyer.
		productListType *cartHead;
		while(!IsCartEmpty)
		{
			//create productListType node with 
			//Product#n from reading the customer file
			productListType *p = readProduct();
			if ( p == NULL )
				IsCartEmpty = true;				
			//add product to cartHead's list
	#ifndef upX86
			createProductList (castHead, p);
	#else
			if (cartHead == NULL)
				castHead = p;
			else
				castHead->next = p;
	#endif
		}
		//Now cartHead should have all prducts bought by 
		//buyer named "*name".
		buyer.setCustomerInfo(name,accNo,cartHead);
		//return new buyer to add in buyer-List
	}
	return buyer ;
}


EDIT:
After all this the main concern is that if you want to use productList prepared by product file for referring in cart (by pointers). But if that list is maintained in a link list and there is not any ID/key to build a relation between both files, it is not possible.

You were not too ambitious, it is defiantly possible but the design and data structure which you have to use is problem.
Last edited on
Topic archived. No new replies allowed.