LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   storing octal value in char variable (https://www.linuxquestions.org/questions/programming-9/storing-octal-value-in-char-variable-435770/)

hubabuba 04-16-2006 06:25 PM

storing octal value in char variable
 
Hi

I am trying to write a program that takes in two arguments: one - fle name, two - permissions in octal form (eg. 0644).
So far I know that for arguments I use:

int main(int argc, char **argv)

then, I want to use:

mode_t mode = argv[2];
int chmod(const char *pathname, mode_t mode)

so my problem is that somehow, I need char argv[2] to store the octal/mode_t value which will need to be assigned to the variable "mode" of type "mode_t". I can't get this to work, please help!

Thanks

paulsm4 04-17-2006 12:53 AM

Several ways:
Code:

  #include <stdlib.h>
  ...
  int istat, imode;
  istat = sscanf (argv[2], "%o", &imode);
  if (istat != 1)
    ...

... or ...
Code:

  #include <stdlib.h>
  ...
  long imode;
  imode = strol (argv[2], NULL, 8);
    ...

'Hope that helps .. PSM


All times are GMT -5. The time now is 09:51 PM.