can't read from simple text file to a struct. (entire program posted)

This is a discussion on can't read from simple text file to a struct. (entire program posted) within the C++ Programming forums, part of the General Programming Boards category; i can not read from a text file into a structure. i must really be missing something big. i haven't ...

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

    can't read from simple text file to a struct. (entire program posted)

    i can not read from a text file into a structure. i must really be missing something big. i haven't had c++ in years, but for this database class he wants this to be the first assignment to see how good we can code (i still have TONS more to do with the data, but i can't even get it to read properly)

    i can open the file properly, just getting it into that struct seems impossible! it does the first 2 or 3 reads properly, after that its all garbage charecters.

    i have put a good 10 hours into this (yes its sad that this is as far as i got) but ive been trying to figure out this problem that long, going through every book/web site i can find, and asking everyone i know. so far i have come up with nothing. i know i am missing something little...

    please help, thanks!

    Jon

    here is the text file:
    refered to as "transaction.h" in my program.
    55 11/10/2002 8015 100 5 home 12345
    56 12/21/2001 8025 150 2 office 34567
    60 08/20/2002 8065 75 1 nursery 12345
    61 03/12/2002 8025 150 2 office 45678
    57 12/01/2002 8035 50 12 office 12345
    58 12/20/2002 8045 200 17 office 56789
    59 10/03/2002 8055 250 17 home 56789
    56 12/21/2001 8045 200 7 office 34567
    60 02/20/2002 8065 75 1 nursery 12345
    58 12/20/2002 8025 150 9 office 56789

    here's my code, almost fully commented:

    Code:
    #include <iostream.h>
    #include <fstream.h>  // Needed to open/close files
    #include <stdlib.h>   // Needed for exit()
    #include <iomanip.h>
    #include <istream.h>
    
    
    ifstream transactionin;  // Allows all functions access to tranaction file
    
    struct Transaction	// Declaration of Transaction Structure
    {
        int ordernumber;
    	char date[10];
    	int orderedproductid;
    	float priceperproduct;
    	int numberordered;
    	char productlineid[10];
    	int customerid;
    };
    
    
    void openfiles()  // This function opens the needed files
    {                 // And also checks to make sure the files open properly
    transactionin.open("transaction.h", ios::in, ios::nocreate); //open transaction.h file
    if (transactionin == NULL)		//check if file opened correctly
    {
    	cout << "\nThe file transactionin.h was not opened, please check the file" << endl;
    	exit(1);
    }
    }
    
    
    void closefiles()  // This function closes the files opened earlier
    {
    	transactionin.close();
    }
    
    
    void readdata()  // This function reads data from the transaction file
    {				 
    
    	const int MAXRECS = 10;	//used to declare amount of space in the structure
    	int i = 0;		//counter to increase the structure
    	
    	Transaction records[MAXRECS] ;	//declaring the structure with MAXRECS number
    
    	
    while(i<=MAXRECS)	//process 10 data records, putting them into the struct.
    	{
    
    		transactionin >> records[i].customerid;
    		transactionin >> records[i].date ;
    		transactionin >> records[i].ordernumber ;
    		transactionin >> records[i].orderedproductid ;
    		transactionin >> records[i].numberordered; 
    		transactionin >> records[i].priceperproduct ;
    		transactionin >> records[i].productlineid ;
    		transactionin >> records[1].customerid ;
    		i++;
    	}
    	
    	cout << records[0].customerid << endl;	//this record prints ok, so does date and order number
    										// but records[0].priceperproduct prints our garbage
    	cout << records[0].productlineid <<endl ; 
    	
    	cout << endl << records[1].customerid << endl;  // prints garbage to the screen
    
    
    	}
    	
    
    
    
    void main()
    {
    	openfiles();  // Call to the openfiles function
    
    	readdata();   // Call to the readdata function
    
    	closefiles(); // Call to the closefiles function
    }
    Last edited by Jon98; 01-31-2003 at 09:46 PM.

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    There is a problem with the way you are reading the text file into the stucture. You are reading in data from the text file, which does not match the field within the structure. Plus you are reading in cutomer id twice... which i don't know why? My advide would be to read in the text file, so it maps directly onto the structure.
    Be a leader and not a follower.

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    This maps better onto the structure. As the producelineid is a char[10] you will need to adjust your text file so there are actaully 10 bytes for it to read in, i.e. "home", this would have to be "home------" within the text file, where '-' are spaces. Hope this helps

    Code:
    for(int Record=0; Record < MAXRECS; Record)
    {
      transactionin >> records[Record].ordernumber;
      transactionin >> records[Record].date ;
      transactionin >> records[Record].orderedproductid ;
      transactionin >> records[Record].priceperproduct ;
      transactionin >> records[Record].numberordered; 
      transactionin >> records[Record].productlineid ;
      transactionin >> records[Record].customerid ;
    }
    Be a leader and not a follower.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    subdene,

    THANK YOU SOOO MUCH!!!!!!!!!!!! it now works! my problem was the 10, so i had to make each thing home------ like you said. i can't beleive i missed that. now i can move on to the "hard" part of this project, sorting the data.

    thanks again

    Jon

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Glad i could help! I'm surprised though that no one else replied to this thread. I had to do a project similar, and reading in the data to a structure can be a pain in the arse at times. It's great when it eventually works! Whilst your doing your sort i'd imagine you are doing it by customer id or order id. It is not necessary to swap each field within the structure, i.e. you can do
    records[2] = records[4]. We weren't told this, and we ended up changing all the fields. Just in case you didn't know that. GOOD LUCK!
    Be a leader and not a follower.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    wow you can do that? i will have to try it. i didn't know how i was gonna do my sort yet (sorted by productid, then count the number of each product sold, producing a report at the end), but i will be reading up on it in alittle bit.

    hopefully i will be able to get the rest done on my own if not, ill be back heh

    thanks again

    Jon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text file to struct help needed!
    By werdy666 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 10:37 AM
  2. Simple File Read Program...
    By PowerHouse in forum C Programming
    Replies: 9
    Last Post: 10-07-2008, 11:30 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21