You have (at least) a couple of issues.
The first is the fact that you generally shouldn't be using an unbounded %s
is scanf
since it does not protect against buffer overflows (very similar to gets
, which was deprecated in C99 and removed from C11).
This is doubly important when your buffers are pitifully small such as char password[1]
which means, with the null terminator, all your passwords will have to be zero characters long. Now I'm no world-famous security researcher but I'm reasonably confident there's a security hole there in that scheme somewhere :-)
There are far safer options when needing user input, such as the function that can be found here, one that protects against buffer overflow and provides information on (and recovers from) the user trying to enter too much data.
The second is relying on uninitialised memory. The theFilePath
variable is not initialised so may contain arbitrary data in it, yet your use of strcat
expects it to contain a num-terminated string. That's a bad assumption to make and can be fixed by simply making the first strcat
into a strcpy
or, since it's always being set to the same initial value, doing that as part of the variable declaration itself:
char theFilePath[512] = "./passfiles/";
And, as an aside, assuming you're using a modern C compiler, you would be better declaring the variables at the point where you need them, rather than all at the top of the function, meaning this declaration should go where the current (errant) strcat
currently is. Localising your declaration and use greatly aids readability.
You may also want to think about the usefulness of the statement:
sprintf(theFilePath,"%s",theFilePath);
Even if it works, it's effectively a no-op since all it does is copy some data to exactly the same place as it currently exists. In any case, it's not guaranteed to work since the standard quite clearly states in C11 7.21.6.6./2
:
If copying takes place between objects that overlap, the behavior is undefined.