Thread: Simple function that's supposed to write to a file - doesn't work

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    9

    Simple function that's supposed to write to a file - doesn't work

    Here's a simple function that's supposed to write to a file (random access file), but it doesn't seem to work. Any ideas why?

    Thanks!!

    Code:
    void addRecord(fstream& file)
    {
    	InventoryItem invItem;
    //	InventoryItem tempInvItem;
    	char tempSerialNum[3];
    	int tempQuantity;
    	double tempPrice;
    //	double tempProfitLoss;
    	long recNum;
    
    
    	cout << "Enter data for the new record: " << endl
    		 << "Serial Number: " << endl;
    	cin >> tempSerialNum;
    
    	
    	recNum = atol(tempSerialNum);
    	if(!validRecNum(recNum))
    		return;
    
    	invItem.putSerialNumber(tempSerialNum);
    
    	cout << "Quantity: " << endl;
    	cin >> tempQuantity;
    	invItem.putQuantity(tempQuantity);
    
    	cout << "Price: " << endl;
    	cin >> tempPrice;
    	invItem.puPrice(tempPrice);
    
    //	file.seekp((recNum - 1) * sizeof(InventoryItem), ios::beg);
    //	file.read((char *)&tempInvItem, sizeof(InventoryItem));
    
    	//if (strcmp(tempInvItem.serialNumber, "") == 0)
    //	{
    		file.seekp((recNum - 1) * sizeof(InventoryItem), ios::beg);
    		file.write((char *)&invItem,sizeof(InventoryItem));
    	//	file.flush();
    		cout << "Record " << invItem.getSerialNumber() << " added to file.";
    //	}
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    You could try using ifstream and ofstream to do what you want instead. There's a tutorial on this site that tells you how to use it.

    http://www.cprogramming.com/tutorial/lesson10.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM