Thread: What's wrong with my read function? read from a txt file to an array of object

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    23

    Exclamation What's wrong with my read function? read from a txt file to an array of object

    i need to read data from my txt file and iv got another function to print, but something must be wrong with my read function b/c my values turn out to be random negatives

    read function
    Code:
    int read(Item Itemlist[], string filename)
    {
        int i=0;
        
        ifstream infile( filename.c_str() );
        while( !infile.eof())
            {
                cout << "read file" <<endl;
                infile>>Itemlist[i].ID;
                infile>>Itemlist[i].sold;
                infile>>Itemlist[i].rem;
                i++;
        }
        return i;
    }
    and the class
    Code:
    class Item
    {
    public:
        string ID;
        int rem;
        int sold;
    };
    a piece of main
    Code:
    Item myArray[100];
        int numOfRecs=read( myArray, "inventory (1).txt");
    I'm almost sure it the problem isn't my print function.

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    My txt file im trying to read from is formatted like this
    Code:
    619847GBE 641 998
    418712IMB 107 867
    227451GEM 789 181
    981836KEA 747 171
    986516IGU 303 71

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    I do know that my program gets into my file because the "read file" does come up on my output but the values i get along with it are not the same values that i should be getting.
    What i needed is "619847GBE" stored in my Item array[i].ID, "641" as my arrays sold int and "998" as my arrays rem int and so on. Could it be the actual reading of it that i need to corrrect?
    Code:
    infile>>Itemlist[i].ID;
    infile>>Itemlist[i].sold;
    infile>>Itemlist[i].rem;
    i++;
    

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You mean like this?
    Code:
    $ ./a.out 
    read file
    read file
    read file
    read file
    read file
    read file
    619847GBE 998 641 
    418712IMB 867 107 
    227451GEM 181 789 
    981836KEA 171 747 
    986516IGU 71 303 
     -964689920 -22220 
    $
    The problem is this
    SourceForge.net: Feof - cpwiki

    Your loop should be
    Code:
    while ( infile>>Itemlist[i].ID ) {
                infile>>Itemlist[i].sold;
                infile>>Itemlist[i].rem;
                i++;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Yes exactly like that! I'm going over the link and changed my loop, but having a little trouble adjusting. Output still the same Changing my code as you read

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Its not working out
    Code:
     -858993460 -858993460
     -858993460 -858993460
     -858993460 -858993460
    Press any key to continue . . .
    thats my output i took off the read

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Solution
    This worked just fine. The mistake was coming from main. Minor error was ruining my read before i even called it. don't i feel embarrassed. but glad it really wasn't my read function
    Code:
    int read(Item Itemlist[], string filename)
    {
    	int i=0;
    	
    	ifstream infile( filename.c_str() );
    	while (!infile.eof() ) 
    	{
    		
    		{
    				infile>>Itemlist[i].ID;
    				infile>>Itemlist[i].sold;
    				infile>>Itemlist[i].rem;
    				i++;
    		}
    		
    	}
    		return i;
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your read function is still broken, if it's still got this in it.
    while (!infile.eof() )

    Study post #4 carefully.
    6 lines of "read file" printed for a 5-line input file, and a garbage last line of data is printed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 35
    Last Post: 12-01-2011, 08:31 PM
  2. Help! What's wrong with my code? How do I read data from a file?
    By adonaldson15590 in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2011, 05:07 PM
  3. Function to read an file.
    By antonis in forum C Programming
    Replies: 4
    Last Post: 10-16-2005, 04:24 AM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  5. Function to read data from file to array
    By anatazi in forum C Programming
    Replies: 3
    Last Post: 07-01-2002, 11:08 PM