Error when passing variables to C function

Pablo Díaz Amores :

The program I am currently working on gets some variables from a text file and uses them to log in into a security camera. I have a config file which contains these variables as following:

192.168.1.30
8000
admin
12345
./var/users/user/files_camera/

I am able to read and print them in the console with:

fp = fopen(filename, "r");
  if (fp == NULL)
  {
        printf("Could not open file %s",filename);
        return 0;
  }
for(int i=0; i<variables; i++)
  {
    if(fgets(str, MAXCHAR, fp) != NULL)
    {
      if(i==0)
      {
        strcpy(ip, str);
      }
      if(i==1)
      {
        sscanf(str, "%d", &port); 
      }
      if(i==2)
      {
        strcpy(user, str);
      }
      if(i==3)
      {
        strcpy(password, str);
      }
      if(i==4)
      {
        strcpy(directory, str);
      }
    }
  }
  fclose(fp);

  printf("%s", ip);
  printf("%d\n", port);
  printf("%s", user);
  printf("%s", password);
  printf("%s", directory);

The thing is that program works when using the following function:

lUserID = NET_DVR_Login_V30("192.168.1.30", 8000, "admin", "12345", &struDeviceInfo);

But not when using:

lUserID = NET_DVR_Login_V30(ip, port, user, password, &struDeviceInfo);

There is no error when compiling, it is just the program not working as expected: the program compiles and executes but the camera returns a login error due to wrong username and password. This is the declaration of the variables:

#define MAXCHAR 50
char ip[MAXCHAR];
int port;
char user[MAXCHAR];
char password[MAXCHAR];
char directory[MAXCHAR];

What am I doing wrong?

NickDoom :
  printf("%s", ip);
  printf("%d\n", port);
  printf("%s", user);
  printf("%s", password);
  printf("%s", directory);

Did you notice you've needed "%d\n" for the port but didn't need "\n" for string parameters? That should give the idea those parameters already end with newline characters. So you pass wrond string to the function, not "12345", but "12345\n".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=6675&siteId=1