Well, I have written this simple program that, from a char array and a keyword, stores in another array the part of the string that begins with that keyword.
It works if i don't use any function to manipulate the array, but what i don't understand is why it doesn't work when it's manipulated through a function, there is no output from printf in this case.
Code that don't work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void createRequest(char *buffer) {
strcat(buffer, "GET /vfolder.ghp HTTP/1.1\r\n");
strcat(buffer, "User-Agent: Mozilla/4.0\r\n");
strcat(buffer, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
strcat(buffer, "Conection: Keep-Alive\r\n\r\n");
strcat(buffer, "name=2&password=3");
}
void getParameters(char *buffer, char *parameters) {
for (int i = 0; i < sizeof(buffer); i++) {
if (strncmp(buffer + i, "name=", 5) == 0) { strcpy(parameters, buffer + i); }
}
}
int main(int argc, char *argv[]) {
char buffer[250];
char parameters[250];
memset(buffer, 0x00, sizeof(buffer));
memset(parameters, 0x00, sizeof(parameters));
createRequest(buffer);
fprintf(stdout, "Your buffer is: \n%s\r\n", buffer);
getParameters(buffer, parameters);
printf("The parameters are: \n%s\r\n", parameters);
}
Code that work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void createRequest(char *buffer) {
strcat(buffer, "GET /vfolder.ghp HTTP/1.1\r\n");
strcat(buffer, "User-Agent: Mozilla/4.0\r\n");
strcat(buffer, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
strcat(buffer, "Conection: Keep-Alive\r\n\r\n");
strcat(buffer, "name=2&password=3");
}
int main(int argc, char *argv[]) {
char buffer[250];
char parameters[250];
memset(buffer, 0x00, sizeof(buffer));
memset(parameters, 0x00, sizeof(parameters));
createRequest(buffer);
fprintf(stdout, "Your buffer is: \n%s\r\n", buffer);
for (int i = 0; i < sizeof(buffer); i++) {
if (strncmp(buffer + i, "name=", 5) == 0) { strcpy(parameters, buffer + i); }
}
printf("The parameters are: \n%s\r\n", parameters);
}