0
Why am I getting an error here? (C++/C)
Code: #include <iostream> #include <fstream> #include <cstring> using std::cout; using std::endl; int main() { std::fstream fileIO; fileIO.open("out.txt", std::ios::app); if (fileIO.is_open()) { char* str; scanf("%s", str); while (strcmp(str, "") != 0) { fileIO << str << "\n"; scanf("%s", str); } } else cout << "Couldn't open file :(" << endl; fileIO.close(); return 0; }
2 Respostas
0
where are you running that code?
pointer str is passed to scanf without initializing it . it has to point to some location on memory for scanf to store data there.
+ 1
Bahha🐧 I'm running that code on Repl.it.
Thanks for the answer, I understand now that I need to either allocate on the heap via malloc/new or type: char str[some bytes].