I know this question has been asked, but the answers I looked at didn't really apply to my case. At the end of my program, a bunch of files are opened for writing. I've limited the list to just two for simplicity. The variable dirPath
is a command line argument passed in at execution.
Here's what I tried first:
FILE *fid_sensory_output;
FILE *fid_msn_output;
fid_sensory_output = fopen(strcat(dirPath,"sensory_output"), "w");
fid_msn_output = fopen(strcat(dirPath,"msn_output"), "w");
This doesn't work because strcat doesn't return a copy of the concatenated strings, but instead appends the 2nd arg to the 1st. When looking up a work around, I found these recommendations, to use strcpy and strcat together, or use sprintf.
I first tried sprintf but was getting an error saying that I was trying to pass an int
in where a char *
was expected, even though dirPath
is declared as a char *
. I also tried passing in a string literal with no luck.
I tried using strcat and strcpy in various ways without any success either. Any suggestions?