I'm trying to search a text file for a string indicating the path of a folder which I will then use as a path to write a text file into. My code above successfully returns the location of the path /home/caars/speech_reg/commands/ to the pointer to find. Now I am trying to strcat the file I'm trying to write into: "mytest.txt" to the path I found and eventually create said file in the very folder (the folder would be /commands). I seem to have a problem copying the string contents of the pointer *find to *fpath or if possible to an array (if it is possible). and reference that array outside the while loop. Meanwhile for some crazy reason, I can't seem to go past the while loop and my output is continuously showing the following:
mytest.txtmytest.txt/home/caars/speech_reg/commmands/
/home/caars/speech_reg/commmands/
which is definitely not what I want. I was looking for something like this:
/home/caars/speech_reg/commmands/mytest.txt
But for some reason the code calls the variable fname twice before fpath or find. Can anyone help me with this? It seems it is a logical problem rather than a syntax one but, I seem to be missing something in the usage of strstr and/or fget. Secondly I am using eclipse C compiler in linux and I keep getting the warnings: "too few arguments for format" when using my printf statements which doesn't affect the code execution but it is a bother. Any ideas? Thanks.
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main(void)
{
FILE *fp = fopen("/home/caars/Desktop/caars.properties.txt", "r");
char *word;
char *fname ="mytest.txt"; //filename
char line[500];
char *find;
char *fpath;
if(fp==NULL) printf("Failed to read input file\n");
else
printf ("file read!!! \n");
while(!feof(fp))
{
word = fgets(line,256,fp);
find = strstr(word,"/home/");
//fpath=find;
printf(find);
fpath=find;
printf(fpath);
printf(fname);
}
strcat(fpath,fname);
printf(fpath);
if(mkdir(fpath,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
{
printf("Failed to create directory %s\n", fpath);
return EXIT_FAILURE;
}
// write to file whose name is spcified with full path
FILE *fx = fopen(fpath, "w");
if (fx == NULL)
{
error(0, errno, "could not open %s", fname);
exit(EXIT_FAILURE);
}
else
{
fprintf(fx, "This is just a test!");
fclose(fx);
}
return EXIT_SUCCESS;
}
/*
HERE I HAVE PASTED BELOW THE CONTENT OF THE caars.properties.txt FILE
# You are reading the ".properties" entry.
! The exclamation mark can also mark text as comments.
speech_recog_output_dir=/home/caars/speech_reg/commmands/
language = English
# The backslash below tells the application to continue reading
# the value onto the next line.
message = Welcome to \
Wikipedia!
# Add spaces to the key
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".
# Unicode
tab : \u0009
*/