I have a peculiar problem in Linux2.4, C and using gcc.
There is a small program to retrieve information from a file using cat & grep cmd.
#define tmpFile "/tmp/cli_tmp_file.txt"
#define MAX_CMD 50
void getRange()
{
char cmd[MAX_CMD+1];
// remove the temp file first
snprintf(cmd, MAX_CMD, "rm -f %s", tmpFile);
system(cmd);
// create temp file
snprintf(cmd, MAX_CMD, "touch %s",tmpFile);
system(cmd);
// execute the command
snprintf(cmd, MAX_CMD, "echo \"Range:Max val 500\" > %s",tmpFile);
system(cmd);
// dump out the temp file so user could see
snprintf(cmd, MAX_CMD, "cat %s|grep \"Range\"", tmpFile);
system(cmd);
// remove the temp file
snprintf(cmd, MAX_CMD, "rm -f %s", tmpFile);
system(cmd);
}
when i execute this code, i get output as cat: /tmp/cli_tmp_file.txt: No such file or directory
However, the file is created in the tmp folder with the contents
# pwd
/tmp
# ls -l
-rw-r--r-- 1 root root 68 Oct 10 12:54 cli_tmp_file.txt
#more /tmp/cli_tmp_file.txt
Range:Max val 500
On manual execution of the same cmd, it displays the intended output
# cat /tmp/cli_tmp_file.txt|grep Range
Range:Max val 500
Any help would be appreciated. Thanks in advance.