I'm having a little big problem with this. What I need to do is create a function that returns a string (receiving another string as parameter). This function should generate a encrypt (sha256) from the input. This is my horrible code and I will explain (or I'll try to)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//not sure if this is the correct way to declare a string function
//with string input parameter, but works with a dummy return
const char* Encrypt (char* Arg1)
{
//varaible to generate command
char command[128];
//variable to store the result
char result[256];
//creating command with input parameter
snprintf(command, sizeof command, "echo -n %s | sha256sum | cut -c1-64",Arg1);
//popen varaible
FILE *fpipe;
//valdiating popen
if (0 == (fpipe = (FILE*)popen(command, "r")))
{
perror("popen() failed.");
exit(1);
}
//here is my problem
char c = 0;
while (fread(&c, sizeof c, 1, fpipe))
{
//when i print te "c", it shows correctly in a line
printf("%c", c);
//but I want to store in "result" variable for using as return
//this doesnt work
snprintf(result, sizeof result, "%s", c);
//this neither
char c2[4];
strcpy(c2, &c);
strcat(result,c2);
snprintf(result, sizeof result, "%s",c);
}
printf("%c", result);
pclose(fpipe);
//return result; not working
return "not woring";
}
Hope u can help me
ok if that is what you want to do just add it to the string as an array:
char c = 0;
resultindex = 0;
while (fread(&c, sizeof c, 1, fpipe))
{
//when i print te "c", it shows correctly in a line
printf("%c", c);
//but I want to store in "result" variable for using as return
result[resultindex] = c;
resultindex++;
}
result[resultindex] = 0;
printf("%s", result);
I didn't test-- it might have bugs
Also, you really should check and make sure that resultindex never gets bigger than 256