I have the code:

#include <fstream> #include <iostream> using namespace std; int main() { const int numArr = 100; char text[numArr]; ifstream file("text.txt"); file.getline(text, numArr); file.close(); for (int i = 0; i < numArr; i++) cout << text[i]; system("pause"); return 0; } 

The text.txt document says:

 Один, два, три 

Why, when I run the program, do I get extra characters in the console (in this case, the letter Mm)?

 Один, два, три ММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММДля продолжения нажмите любую клавишу... 
  • Well, why shouldn't they appear? There are 14 characters in the file, and you have a buffer size of 100. There are not enough characters in the file to fill the entire buffer. And you unconditionally print the entire buffer (all 100 characters), i.e. garbage contained in extra buffer characters. What did you expect? What do you think should be printed for the empty part of the buffer? - AnT
  • Another character, for example - Andrei
  • Your buffer initially contains unpredictable garbage. This garbage you see. If you want to see a “other character” there, then you will have to fill this buffer yourself with this “other character” (in advance or later). For example, if you make memset(text, '%', numArr); before reading memset(text, '%', numArr); , the buffer will be filled with the % character. But it is still not clear to me why you print the entire buffer (100 characters) when only 14 characters were read from the file. Just for the sake of experiment? - AnT
  • Thank! Yes, for the sake of experiment. I am a beginner in C ++ - Andrei
  • Another question, a bit strange. I have two arrays. Is it possible to somehow transfer all elements of the first array to the second, but into one element? That is, all the elements <code> int arro [] = {1, 2, 3}; </ code> are transferred to the second so that there is only one <code> int tarr [] = {123, 4, 5}; < / code>? - Andrei

0