Output of popen returns garbage

I am calling 'find' from popen and the string returned contains garbage at the end. Any ideas on how to fix it?
I am trying to compare output of find to a known dynamically created string.

Here's the output and it's self descriptive; else, I will clarify.
1
2
3
4
5
6
7
8
9
10
login cmd:  "cd .. && find hi_client/stashedclienthi 2>&1" 
 
About to compare: 
 hi_client/stashedclienthi
è  to  hi_client/stashedclienthi 
Output comp:  10 
 comp.c_str():  find: `hi_client/stashedclienthi': No such file or directory 

strlen( hi_client/stashedclienthi ) 25 	strlen( hi_client/stashedclienthi
è ):  28  


Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
   /* Create string to compare w/ find output */
 99         string comp = "find: `";
100         comp.append(fileclient);
101         string comp2 = "': No such file or directory";
102         comp.append(comp2);
103 
104         int res_size = comp.size();
105         char result[res_size];
106 
107         FILE* chk_login = popen(cmd_str, "r");
108         //      fread(result,1,sizeof(result) , chk_login);     
109         fread(result, sizeof(char) ,sizeof(result) , chk_login);
110         fclose(chk_login);
Does the find command work in your shell?

Also, you should use pclose() instead of fclose().

Last edited on
It finds the command fine. What I meant is that the output is returned correctly, but with some garbage values appended at the end of the read string. For example, find would return...
<*file found*><garbage> == <filefound>%&$#@`

I wrote a quick hack and kept just the last index, since I already know the ideal output. So, in any case I would compare the right string or a completely different string.
Well that's odd: I never saw garbage from popen. Have you tried different search strings?
Registered users can post here. Sign in or register to post.