Thread: reading a customer record file

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    1

    Question reading a customer record file

    I have a program due Friday and really need some help. I'm trying to read a file of 500 customer records. Each record consists of a 6-digit customer ID, zip code, 8-digit sales volume, and name. Each field is separated by 5 spaces, with each record on a separate line. When I try to get a total of the records that were read, I get 814, but there are only 500 in the file. Any advice would be greatly appreciated!
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    struct custRec {		
    	int IDnum;
    	int zipCode;
    	int totalSalesVol;
    	char name[20];
    };
    ////////////////////////////////////////////////////////////////////////////////
    void readFile(custRec c[], int arraySize){
    	int i=0;
    	custRec record;
    	ifstream inCustInfo("random.dat", ios::in);
    	
    	if (!inCustInfo){
    		cerr << "File could not be opened."  << endl;
    		exit(1);
    	};
    
    	while (!inCustInfo.eof()){
    		inCustInfo.read((char *)&record, sizeof(custRec));
    		c[i]=record;
    		i++;
    	};
    	cout << "There are " << i << " records in the file.";
    	
    }
    ///////////////////////////////////////////////////////////////////////////////
    main(){
    	const int MAXELEMENTS=1000;
    	custRec customer[MAXELEMENTS];
    	readFile(customer, MAXELEMENTS);
    
    return 0;
    }

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. File Processing using structure and functions
    By Randoon in forum C Programming
    Replies: 1
    Last Post: 12-04-2002, 07:44 PM