hi,
Can anyone tel me how to Save and Load data to and from .txt or binary files in C++?
Printable View
hi,
Can anyone tel me how to Save and Load data to and from .txt or binary files in C++?
GIYF.
Or even this could get you started (although it seems like a poor example).
hey MacGyver, thanks!
i checked the example...i think it will do for now :-)
By the way... GIYF ?!?!?!
JFGI.Quote:
By the way... GIYF ?!?!?!
Yeah, STFW.
And don't forget to RTFM while you're at it.
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?
Stop using a subscript out of the vector's range?
"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...
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
but where in the code am i subscripting?
must i use the reserve() function to resolve this?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);
}
"Artificial Intelligence usually beats natural stupidity."
So true...so true.
sorry...
i've figured it out
used:
thanks for the help guys :-)Code:dadosProp.reserve(50);
dadosVeiculo.reserve(50);
if(dadosProp.empty() == false)
{
for(int i=0; i>=50; i++)
That's not really solving your problem, despite the fact that it may appear to work normally. The problem is that when you call dadosProp[i], for example, it's assuming that that element already exists - which means that the vector's size must be big enough. Using reserve() sets the capacity, but not the size. You can create a vector of size 50 by calling the appropriate constructor. For example, if dadosProp is a vector of ints,
Alternatively, if you are going to write elements of the vector sequentially starting at index 0, you canCode:std::vector<int> dadosProp(50);
do it using push_back() (I prefer this since it avoids the often unnecessary initialization which the above constructor performs on the elements). If you do this, you can still call reserve() as an optimization if you have an upper bound on how big dadosProp can get, but it's not necessary for correctness.
To see the problem with your original code, if you're compiling with GCC, try using the -D_GLIBCXX_DEBUG option, which causes bounds checking on each vector access when you run the program.
http://gcc.gnu.org/onlinedocs/libstdc++/debug.html
Edit: Actually in the case of a vector of a primitive type such as int, the overhead of push_back()'s having to modify the size is probably just as bad as the initialization in the constructor. But push_back() is safer when you can use it, since it's guaranteed not to cause invalid writes such as yours. On the other hand, if the writes need to be done in any order other than sequentially starting with index 0, you have to make sure the vector has large enough size (not just capacity) when you create it.
ups!!!
i didn't copy the right code :-S
sorry guys.
Code:dadosProp.reserve(50);
dadosVeiculo.reserve(50);
if(dadosProp.empty() == false)
{
for(int i=0; i<50; i++)
As I explained, your code is broken. Using reserve() is an optimization only - it doesn't affect correctness. If you compile using GCC with the option mentioned above, or alternatively replace [] with at() for subscripting to do bounds checking with each access, your program will crash with a complaint about out-of-bounds access, since reserve() only sets the capacity, and doesn't change the size, which remains at 0. You could fix it by using resize() instead of reserve(), or more simply just use the constructor which sets the size at creation.
I was confused about what GIYF is and so I thought I would Google it. Then I realized - OOOOOOOOh, Google is your friend.
I still didn't know what JFGI, STFW, and RTFM were, but using the advice from the first one, I was able to find the others. It turns out I had heard of RTFM before.