Thread: Problem with strcat

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    14

    Problem with strcat

    My code compiles without any errors or warnings but when I run it I get "app.exe has stopped working" and it never returns 0 to the build log (I get "Process terminated with status -1073741819").

    It only works if I delete the line "strcat(dir, "/AppData/Roaming/File.txt");"

    Anyone knows what this could be?

    Here's my code

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <windows.h>
    #include <Lmcons.h>
    
    using namespace std;
    
    int main(void)
    {
        char user[UNLEN + 1];
        DWORD len = UNLEN + 1;
        GetUserName(user, &len);
    
        char dir [] = "C:/Users/";
        strcat(dir, user);
        strcat(dir, "/AppData/Roaming/File.txt");
    
        fstream file;
    
        file.open(dir, ios::out | ios::app);
        file << "File created!" << endl;
        file.close();
    
        return false;
    }
    The file is created in the directory even though I'm getting that error...
    Last edited by wtd81; 01-16-2016 at 11:58 AM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    strcat will only work if there is sufficient space in the array. In the manner that you have declared dir, there will only ever be space for the initial string, "C:/Users/" and no room for tacking on.

    I recommend strings instead:
    Code:
    string dir = "C:/Users/";
    dir += "AppData/Roaming/File.txt";
    file.open(dir.c_str(), fstream::out|fstream::app);
    ...
    It may work out that you don't need c_str() at all. Later versions of fstream accept string type paths. Give it a try.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2015
    Posts
    14
    Thanks you both, its working now.

    Just for curiosity, I tried exactly the same thing in C and it worked without any problems even if I don't declare a size to the array, any ideias why?

    Also, your code was really helpful, laserlight, thanks for taking the time to explain it. Insightful!
    I'm starting with C++ after some time studying C, I just felt it was time to move on, learn more about OOP, etc, so I don't know much C++ features yet, I see now I still have much to learn :}

    Just so I can get it all together, is there any problem about using "return false"? I just thought it would be the same thing as return 0
    Last edited by wtd81; 01-16-2016 at 04:47 PM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by wtd81 View Post
    Just for curiosity, I tried exactly the same thing in C and it worked without any problems even if I don't declare a size to the array, any ideias why?
    A fluke. The code is undefined behaviour which means it can do anything including crash, overwrite other variables, or do what you wanted it to do.

    Just so I can get it all together, is there any problem about using "return false"? I just thought it would be the same thing as return 0
    No problem. It's just the return type is an int, so it makes more sense to use 0 than false.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat problem
    By javaeyes in forum C Programming
    Replies: 6
    Last Post: 02-27-2012, 12:30 PM
  2. strcat problem
    By mindtrap in forum C Programming
    Replies: 5
    Last Post: 08-10-2007, 02:39 AM
  3. STRCAT problem
    By Frusciante in forum C Programming
    Replies: 9
    Last Post: 07-04-2007, 06:15 AM
  4. Problem with strcat
    By firyace in forum C Programming
    Replies: 9
    Last Post: 05-15-2007, 02:31 PM
  5. problem with strcat
    By Nanook in forum C Programming
    Replies: 7
    Last Post: 09-04-2005, 03:51 AM