How to get variable to linux command from config file?

Hi everyone. I have a code script that deleting unused logs.
I have this function for it. +5 represents the days. I want to get it from outside, from a config file. How can i do it?

system("find /var/log -name '*.log' -type f -mtime +5 delete");

Crudely, but the basic idea:
1
2
3
4
FILE *fp = fopen("config","r");
fscanf(fp,"%d",&days);
sprintf(buff,"find /var/log -name '*.log' -type f -mtime +%d delete",days);
system(buff);
Im getting errors

'buff' was not declared in this scope
'days' was not declared in this scope
And it gives me an segmentation fualt error. What can be the reason?
As c with no error checking perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>

int main() {
	FILE* fp = fopen("config", "r");
	int days = 0;

	fscanf(fp, "%d", &days);

	int req = snprintf(NULL, 0, "find /var/log -name '*.log' -type f -mtime +%d delete", days);
	char* buff = (char*)calloc(req + 1, 1);

	snprintf(buff, req + 1, "find /var/log -name '*.log' -type f -mtime +%d delete", days);
	system(buff);
	free(buff);
}

I compile it with g++ in my linux.
It gives me the error : Segmentation fault (core dumped)
Ok. So trace through the code using the debugger. Which line is giving the problem?

What is the value of days after L8? What is the value of req after L10? What are the contents of buff after L13?
> I compile it with g++ in my linux.
> It gives me the error : Segmentation fault (core dumped)
Do you have a file called config?
Does it contain what you want?
Did it open the file correctly?

> 'buff' was not declared in this scope
> 'days' was not declared in this scope
See, I assumed you would know how to fill in the blanks and make the appropriate changes.

It seems though you're looking for copy/paste answers that work out of the box.
@suslucoder, this was just an idea, you're expected to implement it yourself.

The code probably is failing because you don't have a file called "config" that your code can see, and fp has the value NULL.
Thank you for all your answers. I was written my file name wrong...
Sorry for keeping you busy
Topic archived. No new replies allowed.