i want to read the first line from text.txt the number gotten will define the size of the array
and the 2nd integers and etc go the array (size of array given above)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
int main()
{
string arraySize;
string line;
int size;
int i;
ifstream myfile ("text.txt");
if(myfile.is_open())
{
getline(myfile, arraySize);
while(myfile.good())
{
getline(myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
|
eg.. file text.txt will have the following numbers in it.
5
11 10 9 4 33
so
5 will be the array size and the rest of the numbers on the 2nd line (
11 10 9 4 33) will be inserted into the array
anyone knows how can i do the needed? thanks.
Last edited on
getline reads the whole line (as the name suggests) into a string. To read an integer, use operator >>.
First read the size, then in a for loop (i = form 0 to size) read i'th item in the array.
any examples? thanks. i am new to this kind of concept. no matter how i do i still cant get it.