I am trying to read a file and put data into string. However the compiler is outputting this.
012 345 678 ����012345678 with the new lines. Can you explain what is happening?
#include <iostream> using namespace std; #include <fstream> #include <cstring> int main() { ofstream output("transform.out"); ifstream input("transform.in"); int num = 0; input >> num; char tmp[num+1]; char data[num * num +1]; while(input >> tmp){ cout << tmp << '\n'; strcat(data, tmp); } cout << data; } transform.in has this data
3 012 345 678
char tmp[num+1];is not valid C++. Use a vector or better yet std::string.char tmp[num+1]and thischar data[num * num +1]are invalid syntaxes, the array size defined inside the square brackets must be a constant value.