This line declares dir to be an array of char with just enough space to store the null terminated string "C:/Users/" to which it is initialised:
Code:
char dir [] = "C:/Users/";
Therefore, you cannot strcat anything to it. Besides, strcat is for C-style null terminated strings.
Seeing that you may be programming to a version of the C++ standard prior to C++11, I suggest:
Code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <windows.h>
#include <Lmcons.h>
using namespace std;
int main(void)
{
char user[UNLEN + 1];
DWORD len = UNLEN + 1;
if (GetUserName(user, &len))
{
stringstream ss;
ss << "C:/Users/" << user << "/AppData/Roaming/File.txt";
fstream file(ss.str().c_str(), fstream::out | fstream::app);
file << "File created!" << endl;
}
}
Notice that:
- I check the return value of GetUserName in case it fails.
- I use a stringstream instead of strcat.
- I open the file via the fstream object constructor.
- I do not explicitly close the file because it will be closed when the fstream object is destroyed.
- I did not return anything from the global main function as the return 0 is implicit as a special case. If I did want the return statement, then return 0 rather than return false would be correct.