ファイルから配列を読み込もうとしています。しかし、私は私の配列のサイズ= 5を割り当てています。ファイルの大きさにかかわらず、それはサイズとそれを停止してから、私はすべての要素をコピーしたい場合は、サイズ= 10の新しい配列に読み込まれ、私の新しい配列にファイルを完全に読み込んでいます。新しい配列がそのサイズの2倍になるたびに同じことをしてください。 qoustionは現在の配列がいっぱいであるかどうかを判断する方法です。たとえば、配列がいっぱいかどうかを判断する方法
ifstream in;
while (in.fail())
{
cout << "Exit" << endl;
return 9;
}
int size =5;
int* array;
int* resize;
int inc=0;
array = new int[size];
while (in << *(array+inc))
{
inc++;
/// here i want to detrmine if it's full to do the rest of code
}
PrintArray(resize,size)
ヒントやヒントがあります。
どのように動作するようなものを取得する。
while (in >> *(array+inc)
{
if (inc <=5) // if it reaches the size which is 5
{
for (int i=0; i<size i++)
{ // to copy all elements to the new array
resize[i] = array[i]
}// delete the old array
delete[] array;
while (in >> *(resize+size)) // to keep reading the file where it's stoped
}
PrintArray(resize,size) // print it
delete[] resize; // delete the new array and so on
in.close();
'inc 'が5の時に完全な、あなたは? – immibis
あなたは配列を割り当てなかっただけで、 'int * array'でランダムなメモリ位置を指すポインタを持っています。 –
while(inc <= 5) – Muzy