Upload data from .txt into std::set

This is a discussion on Upload data from .txt into std::set within the C++ Programming forums, part of the General Programming Boards category; hi, i'm trying to upload data from a .txt file into a std::set. i saved the data from the std::set ...

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    Upload data from .txt into std::set

    hi,

    i'm trying to upload data from a .txt file into a std::set.
    i saved the data from the std::set into the .txt file successfully, but know, when i do the upload into the std::set the data is messed up :-(

    this is what happens...

    saved .txt file (everything ok):
    Code:
    //'\n'
    ID
    type
    detail
    cost
    weight
    uploaded data into the std::set:
    Code:
    Product 1:
    
    
    
    cost
    weight
    
    Product 2:
    ID
    type
    detail
    cost
    weight
    this is my upload function:
    Code:
    void grocery::loadProducts(string nameFile)
    {
    	...
    	...
    
    	while(data)
    	{
    		data.ignore (100,'\n');
    
    		data.getline (str,100);
    		i = str;
    		...
    		...
    		data>> c;
    
    		...
    
    	}
    }
    what am i doing wrong ??
    Last edited by IndioDoido; 11-02-2007 at 08:46 PM.
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Is that what the txt file looks like exactly? p and c are integers, but there are no numbers there. The txt file you output should have numbers for p and c.

    Also, you should have a data.ignore() line after the calls to read in p and c. That's because p and c are on their own line, and if you don't do that then the newline will screw up the call to getline later in the loop.

  3. #3
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi again Daved!

    that is just an example, the real values are something like this:

    123BRD
    bread
    healthy nice bread
    100
    10

    I did what you asked and it continues the same :-(

    Code:
    while(data)
    	{
    		data.ignore (100,'\n');
    
    		data.getline (str,100);
    		i = str;
    
    		...
    		...
    
                    data.ignore (100,'\n');
    
    		...
    
    	}
    std::set after upload:
    Code:
    Product 1:
    
    
    
    100
    10
    
    Product 2:
    123BRD
    bread
    healthy nice bread
    100
    10
    Last edited by IndioDoido; 11-02-2007 at 08:45 PM.
    "Artificial Intelligence usually beats natural stupidity."

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    So you have a text file that looks correct. Then you read it in, then you write it out. Then it looks like that. Is that correct?

    Can you give the exact input and output files, even if they are in a different language?

    It looks like there are is an empty line between the 10 and the 123BRD. That means you have to ignore the newline after 10, ignore the newline in the empty line, and ignore the Product 2: line. You are only ignoring two of those. Maybe a second call to ignore below the one you just added will help. It's hard to tell without seeing exact input and output. Those ignore lines are important, so if they aren't exactly right then you will not get valid data.

    If you want, you can also cout the data you read in as you go to see if you are getting the data correctly.

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Correct me if I'm wrong, but does the code in red actually work:
    Code:
    while(data)
    	{
    		data.ignore (100,'\n');
    
    		data.getline (str,100);
    		i = str;
    		data.getline (str,100);
    		t = str;
    		data.getline (str,100);
    		d = str;
    
    		data>> p;
    		data>> c;
    
                    data.ignore (100,'\n');
    
    		produtos insertProduct(i, t, d, p, c);
    
    		Prod.insert(insertProduct);
    
    	}

  6. #6
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    Daved is this what you want:

    saved .txt file:
    (empty line)
    123BRD
    bread
    healthy nice bread
    100
    10
    viewing uploaded data on the program:
    Product 1: (instead of showing only one product, it show's 2, and the Product 1 only has the integer values)
    (empty line)
    (empty line)
    (empty line)
    100
    10

    Product 2:
    123BRD
    bread
    healthy nice bread
    100
    10

    Press any key to continue ...
    upload data to std::set from .txt file:
    Code:
    void grocery::loadProducts(string nameFile)
    {
    	...
    	...
    
    	while(data)
    	{
    		data.ignore (100,'\n');
    
    		data.getline (str,100);
    		i = str;
    
    		...
    		...
    
    	}
    }

    I think the code in red works, because it actually gets the values from the .txt file and inputs them in the std:set.
    Last edited by IndioDoido; 11-02-2007 at 08:44 PM.
    "Artificial Intelligence usually beats natural stupidity."

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I think I see the problem. Your loop that reads in the data is not stopping at the correct point. You want to stop when there is no more data in the file. You should ignore the empty line before the loop. Then put the call to getline inside the while control. Instead of while (data), you should use while (getline(data, i)). That way, the loop will stop when the getline cannot read in a new product.

    The way you have it now, the while (data) is true after the first product, but then the getline fails and the other reads fail. The strings are left empty and a product is inserted with empty strings and the old data.

    >> Correct me if I'm wrong, but does the code in red actually work:
    Yes it does, but now that you mention it IndioDoido should use the string version of getline and get rid of str altogether:
    Code:
    	while (getline(data, i))
    	{
    		getline (data, t);
    		getline (data, d);

  8. #8
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ok...
    i removed this from the save std::set to file function:
    Code:
    file<< '\n'; //removed!!
    while(dataProduct!= Prod.end() )
    {
    	... ...
    }
    and changed the save std::set to file function to this:
    Code:
    while(getline(data, i)) //removed data
    {
    	
    	getline (data, t); //using getline()
    	getline (data, d);
    
    	...
    	...
    }
    And now after upload and viewing the data on the program:
    Product 1:
    bread
    healthy nice bread
    100
    10
    3435973836 //it show's this number, and the id (123BRD) value that should appear at first doesn't show :-(

    Press any key to continue ...
    damm! i thought working with std::set's and files was easy. err... :-S
    Last edited by IndioDoido; 11-02-2007 at 08:42 PM.
    "Artificial Intelligence usually beats natural stupidity."

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Code:
    while(getline(data, i)) //removed data
    {
    	getline(data, i); //moved to getline()
    In that code you're reading i twice, you only should read it once, inside the while control. If that fist read fails then the loop will stop. If it succeeds, then i will have the product id, so there's no need to read into i again.


    There's one last thing you're going to have a problem with. This is an example of your text file with the newlines shown if you had two products:
    Code:
    123BRD\n
    bread\n
    healthy nice bread\n
    100\n
    10\n
    345GRP\n
    grapes\n
    juicy green grapes\n
    200\n
    20\n
    Focus on the bold parts. In your code this line will read in the 10:
    Code:
    data>> c;
    That will leave the text to look like this:
    Code:
    \n
    345GRP\n
    ...
    The next time your loop runs, it will call getline to read a line. The getline function stops when it sees the newline character '\n'. So it will read an empty string into i and then get rid of the '\n' leaving this:
    Code:
    345GRP\n
    ...
    But that's not what you want, you want it to read in 345GRP.

    The solution is to ignore the \n after the 10. So after you read into c, you should ignore a line so that getline won't get messed up.

    If you make that fix, and the fix I mentioned at the top of this post, it should be better.

  10. #10
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    YYYUUUUUUPIIIIII...

    Thank you Daved! it's finally working nicely
    The first part of my assignment is finished.

    Thanks to you, tomorrow i will start the second and final part of my assignment ;-)
    "Artificial Intelligence usually beats natural stupidity."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 07:04 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 02:22 PM

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