Thread: Save and Load memory data to Files in C++ ?

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

    I/O Binary Files in C++

    hi,

    Can anyone tel me how to Save and Load data to and from .txt or binary files in C++?
    Last edited by IndioDoido; 10-15-2007 at 06:35 PM. Reason: I/O Binary Files in C++
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    GIYF.

    Or even this could get you started (although it seems like a poor example).

  3. #3
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hey MacGyver, thanks!

    i checked the example...i think it will do for now :-)

    By the way... GIYF ?!?!?!
    "Artificial Intelligence usually beats natural stupidity."

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    By the way... GIYF ?!?!?!
    JFGI.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yeah, STFW.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    And don't forget to RTFM while you're at it.

  7. #7
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    what the helll !?!?!?!

    i'm lost here :-(

    by the way...
    i'm getting this error:
    Expression: vector subscript out of range

    how can i resolve this?
    Last edited by IndioDoido; 10-16-2007 at 06:44 PM. Reason: New Question :-D
    "Artificial Intelligence usually beats natural stupidity."

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Stop using a subscript out of the vector's range?

  9. #9
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    "Stop using a subscript out of the vector's range?"

    how?

    the error starts here:

    Code:
    if(dadosProp.empty() == false) //-> ERROR
    	{
    		for(int i=0; dadosProp.size(); i++)
    		{
    			if(dadosV.getNumBiProp() == dadosProp[i].getBI() )
    				dadosVeiculo.push_back(dadosV);
    			else 
    				{
    					cout<<"Proprietario não existe! Insira um novo...";
    					system("pause");
    					system("cls");
    
    					preencheProprietario(dadosP);
    					inserirProp(dadosP);
    				}
    		}
    	} else etc..etc...
    "Artificial Intelligence usually beats natural stupidity."

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    Code:
    int array[9];	// you now have an array of 9 int's
    
    array[9] = 5;	// You can't do this as you are out of the array's range
    		// You only have 0-8, so if you call -1 and beyond or 9 and beyond you
    		// are trying to access memory that the array does not have allocated.
    		// Thus you get subscript out of range error, same with vectors
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  11. #11
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    but where in the code am i subscripting?
    Code:
    if(dadosProp.empty() == false)
    	{
    		for(int i=0; dadosProp.size(); i++)
    		{
    			if(dadosV.getNumBiProp() == dadosProp[i].getBI() )
    				dadosVeiculo.push_back(dadosV);
    			else 
    				{
    					cout<<"Proprietario não existe! Insira um novo...";
    					system("pause");
    					system("cls");
    
    					preencheProprietario(dadosP);
    					inserirProp(dadosP);
    				}
    		}
    	} else 
    			{
    				cout<<"\nInsira o novo proprietario...\n";
    				system("pause");
    				preencheProprietario(dadosP);
    				inserirProp(dadosP);
    				dadosVeiculo.push_back(dadosV);
    			}
    must i use the reserve() function to resolve this?
    "Artificial Intelligence usually beats natural stupidity."

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by IndioDoido View Post
    but where in the code am i subscripting?
    If you can't answer this question, stop coding whatever you're working on, and figure this one out.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    "Artificial Intelligence usually beats natural stupidity."

    So true...so true.
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  14. #14
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    sorry...
    i've figured it out

    used:
    Code:
    dadosProp.reserve(50);
    dadosVeiculo.reserve(50);
    
    	if(dadosProp.empty() == false)
    	{
    		for(int i=0; i>=50; i++)
    thanks for the help guys :-)
    "Artificial Intelligence usually beats natural stupidity."

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by IndioDoido View Post
    Code:
    for(int i=0; i>=50; i++)
    Well, that isn't right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. File Save and Load
    By lukesowersby in forum C Programming
    Replies: 3
    Last Post: 05-13-2009, 12:49 AM
  3. Replies: 11
    Last Post: 04-17-2008, 02:29 AM
  4. loading files, and memory allocation
    By Rard in forum C Programming
    Replies: 14
    Last Post: 04-20-2006, 03:22 PM
  5. Replies: 3
    Last Post: 09-12-2005, 09:08 AM