I see a number of problems with this code:
allocating character
using malloc()
instead of new[]
, and then not reading anything into character
before writing it to file, and not deallocating character
.
opening TEXT
with ios::in
, not reading anything from it, and then reopening it with ios::out | ios::app
without close()
'ing it first.
reading user input into the memory that Name
points to, which may or may not be large enough to hold the user input, or may be pointing at read-only memory, etc.
It is really difficult to figure out what your code is trying to do. But I think you might try something more like this instead:
void copyFile(fstream &TEXT, char *Name) {
TEXT.close();
TEXT.open(Name, ios::in);
ostringstream ss;
ss << TEXT.rdbuf();
TEXT.close();
cout << "Enter File Name: ";
string otherName;
getline(cin, otherName);
TEXT.open(otherName.c_str(), ios::out | ios::app);
TEXT << " " << ss.str();
cout << "Done copying..\n";
}
Or, maybe you want something more like this:
void copyFile(fstream &TEXT, char *Name){
ifstream ifs(Name);
cout << "Enter File Name: ";
string otherName;
getline(cin, otherName);
TEXT.close();
TEXT.open(otherName.c_str(), ios::out | ios::app);
TEXT << " " << ifs.rdbuf();
cout << "Done copying..\n";
}
Or, maybe simply this:
void copyFile(fstream &TEXT, char *Name) {
ifstream ifs(Name);
TEXT << " " << ifs.rdbuf();
cout << "Done copying..\n";
}