Below is my c code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
char *username;
char *password;
void set_credentials(char*, char*);
int main(void)
{
set_credentials();
printf("%s\n", username); //look here 3
printf("%s\n", password); //look here 4
return EXIT_SUCCESS;
}
void set_credentials(char *username, char *password)
{
char c;
char lines[2][100];
char * tmp = * lines;
char * user = "user";
int i = 0;
FILE *fp = fopen("/netnfork/config/netnfork_credentials.properties", "r");
if (fp == NULL)
exit(EIO);
while ((c = fgetc(fp)) != EOF)
{
if (c != '\n')
{
*tmp = c;
tmp++;
} else {
*tmp = '\0';
i++;
tmp = lines[i];
}
}
fclose(fp);
i = 0;
while (i < 2)
{
if (strncmp(user, lines[i], 4) == 0)
{
username = lines[i] + 5;
printf("%s\n", username); //look here 1
} else {
password = lines[i] + 9;
printf("%s\n", password); //look here 2
}
i++;
}
}
Now, when I run the code, I get this:
myname //for 1
mypassword //for 2
myname // for 3
mypasswo��� // for 4
I can’t understand why that behavior. Have anyone ideea about why that?
Here is a version that uses malloc() and free().