Originally Posted by
CommonTater
As a matter of fact, some of the very best are right here...
You want to know how to copy a string to a string... look up strcpy() as suggested in your C library documentation.
To locate the : and get your starting point you probably want something like strchr() which you should also look up in your docs...
How to make this function work and create the target file :
rename("/var/tmp/temp.cfg", line); //not working
Code:
FILE *fsp,*fdp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fsp = fopen("/var/etc/allInOne.cfg ", "r");
fdp = fopen("/var/tmp/temp.cfg", "w");
if (fsp == NULL)
exit(EXIT_FAILURE);
if (fdp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fsp)) != -1) {
//printf("Retrieved line of length %zu :\n", read);
//printf("%s", line);
if (strstr(line, "/var/")) {
//Close the fdp file
fclose(fdp);
//get the path from line here
//how can i extract the path here and pass it to the rename function
//Move the temp file
rename("/var/tmp/temp.cfg", "/var/etc/file1.cfg"); //work good
//rename("/var/tmp/temp.cfg", line); //not working
//Delete the temp file
remove("/var/tmp/temp.cfg");
//open the temp file
fdp = fopen("/var/tmp/temp.cfg", "w");
if (fdp == NULL)
exit(EXIT_FAILURE);
}
else {
fprintf(fdp,"%s",line); /*writes data to the file*/
}
}
//close the source file
fclose(fsp);
fclose(fdp);
//free the pointer line
free(line);