user1888502
user1888502

Reputation: 373

sys open not working with pointer in c

I have : Now if instead of input in sys_open(), if I were to pass a.txt, it works. But I need to get the username for command line therefore I have to copy it to input. when I pass my pointer variable, it does not work. Why?

int main()
{
    char *name;

    char input[1024];
    strcpy(input, argv[1]);

    name = input;

    sys_open(input, "O_RDWR", 00700);

}

Upvotes: 1

Views: 814

Answers (2)

Ganesh
Ganesh

Reputation: 5990

Could you try with sys_open(input, O_RDWR, 00777);. I have modified this code as below and it is working for me

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
char *name;
int   fd;
int   data = 0;

char input[1024];
strcpy(input, argv[1]);

//name = input;
fd = open((const char *)(input), O_RDWR, 00700);
printf("file descriptor: %x\n", fd);

read(fd, &data, 2);
printf("Data: %d\n", data);

return 0;
}

Upvotes: 1

user149341
user149341

Reputation:

The flags to open (I'm not sure why you're referring to it as sys_open) are passed as a symbolic constant, not a string.

open(input, O_RDWR, 00777);

You will almost certainly need to assign the return value somewhere to do anything useful.

Upvotes: 3

Related Questions