[Solved] What is a better way of reading a file in c++? [closed]


Either can work perfectly well. The real question is what you want to do with the data after you read it — if your processing is oriented toward complete lines, then read complete lines. If it’s oriented toward words, then read words. If it’s oriented toward characters, then read single characters.

Just for example, if you wanted to read a file and capitalize every letter it contained, it would probably be easiest to read a character at a time, capitalize it, write it out, and repeat.

If you wanted to capitalize the initial letter in ever word in the file, it would probably be easiest to read a word, capitalize the first letter, write out the word, and repeat.

Neither is really better, worse, more elegant, etc., than the other — it’s purely a question of choosing what suits what you’re trying to do. One might be marginally faster than the other, but that’ll depend heavily on the standard library you’re using, and there’s little (if any) way to predict which will be faster in general.

2

solved What is a better way of reading a file in c++? [closed]