The code has undefined behavior. When you do something like this:
char ifsw1w[3]; ifsw1 >> ifsw1w;
operator>> gets a pointer to the buffer, but has no idea of ββthe actual size of the buffer. Thus, he cannot know that he should stop reading after two characters (and note that this should be 2, not 3 - to complete the line βrequiredβ for β\ 0β).
Bottom line: When learning how to read data, this code is probably best ignored. What you can learn from code like this, you should avoid. However, as a rule, it is easier to simply follow a few rules of thumb than to try to study all the problems that may arise.
- Use std :: string to read strings.
- Use only fixed buffers for fixed size data.
- When you use fixed buffers, skip their size to limit the number of reads.
If you want to read all the data in a file, std::copy can avoid many errors:
std :: vector strings.
std :: copy (std :: istream_iterator (Myfile), std :: istream_iterator (), std :: back_inserter (lines));
source share