1

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?

  • 2
    Try printing the values of those variables right before the function call to make sure they contain what you think they do. – dbush Feb 18 '20 at 19:33
  • 3
    `fgets` result includes newline character in the end. You need to remove those before copying strings. – Alex Skalozub Feb 18 '20 at 19:35

2 Answers2

0

As Alex Skalozub said, fgets function includes newline character at the end and I was not keeping that in mind. This is what I did to solve the problem:

char buffer[MAXCHAR];

strcpy(buffer, str);
strncpy(ip, buffer, strlen(buffer)-1);

Used an extra variable to read each line and remove the \n character at the end of each variable.

Now it is working! Thank you!

  • No intermediate copy is needed here, you may `strncpy` directly from `str` buffer. Also note that newline terminator could be platform-specific, so you also need to handle that if you want your code to be cross-platform. Generally, trimming all whitespace (`'\r'`, `'\n'`, `'\t'`, `' '`) characters is a good idea (you may use builtin `isspace` function for that). – Alex Skalozub Feb 18 '20 at 19:50
0
  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".

NickDoom
  • 339
  • 1
  • 9