Just getting started with C++, done a bit of C but one thing I don’t get is that you can’t substitute >> for =?
My code below:
char output[100];
if (myReadFile.is_open())
{
for(int i=0; i != random_integer; i++)
{
if(i == random_integer-1){
myReadFile >> output;
printf("%s",output);
}
}
}
myReadFile.close();
I’d like to change myReadFile >> output to output = myReadFile but you can’t do that?
My second question is, I want to measure the length of the string that I assign to output, how can I do that without looping through the whole char array?
TIA
The reason you can’t use
=instead of>>is because they’re two unrelated operators.To solve the string-length issue, don’t use a raw
chararray. Use astd::stringinstead, because it manages its own memory internally: