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


*/

Recommended Answers

All 4 Replies

This version creates the right path

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");
	    return EXIT_FAILURE;
      }

	printf ("file read!!! \n");
    while(word = fgets(line,256,fp))
    {
        find = strstr(word,"/home/");
        fpath=find;
        if( find != NULL)
        {
            if( fpath[strlen(fpath)-1] == '\n')
                fpath[strlen(fpath)-1] = 0;
            break;
        }
    }
    fclose(fp);
    strcat(fpath,fname);
    printf("%s\n", fpath);
}

wilhemina> 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?

It is just a warning since normally, printf() expects a literal string and some formatting option. In that case it would be an alternative option of using just puts() with fpath, fname, and find pointers to string.

while(!feof(fp))

feof() is not designed in C to be used as a stop control for a loop. Bottom line is that the eof does not reach the while() as you expected.

Awesome. The code above works! thanks!! You're right Aia. After some reading around I had already resorted to using puts(). But thx for the heads up on feof. That does make sense because as I stepped through each of the variables using breakpoints in eclipse, I saw that the variables were registering null values within the while loop so I was not getting the assignments I was expecting. Thanks a lot guys!

I am using TextFinding.com to search for text files on my pc and other networked computers. It is working fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.