I am developing a program in which I have to read a text file and store the integer values in an array from the text file. I have searched for how to do and found this method. But the result is not as expected.
For instance, say, I have integer values in a text file called 'integers.txt' such as 1,2,3,4,5,6,7,8,9,10. I am reading those values and add it one by one with the new array called 'myarray'. But after performing an addition, using while loop, I have got only zero as output in 'myarray'
Here is my code:
int main(int argc, char **argv)
{
int myArray[2048]={0};
int value;
int i = 0;
while(file >> value && i<sizeof(myArray))
{
myArray[i] += value;
i++;
std::cout<<myArray[i]<<std::endl;
std::cout<<value<<std::endl;
}
std::cout<<i<<std::endl;
std::cout<< sizeof(myArray)<<std::endl;
return 0;
}
Thanks for the help!