سؤال

I'm working on a C program with some system calls and I'm saving variables as follows:

int inhandle,outhandle,bytes,read_while_writing_nhandle;
char source[128],target[128];

Right now my program prompts the user with

printf("enter source file name\n"); 
scanf("%s",source);

How can I change it so a user can just type "run sourceName targetName" aka, storing them as argv[1] and argv[2]?

I also use the inputs (target and source) in handles like:

inhandle=open(source,O_RDONLY);

My main issue is conversion since I'm storing target and source as char. I could use something like strcpy. It would just be very much appreciated if someone could help me out with it. Hope this was clear. Thank you.

*********EDIT: I apologize, I probably wasn't clear enough...

I tried doing the int main(int argc, char *argv[]) and then including:

if(argc==3) 
{ source = argv[1]; target = argv[2]; } 
else{ printf("Syntax error.\n"); 
return -1; 

But I'd get conversion errors since I can't store them that way. And if I do store them as pointers (like *target=argv[2]) I'm worried they won't work when I call the handles.. (ex: outhandle=open(target...) works but I can't do open(*target..)

هل كانت مفيدة؟

المحلول 2

Change to:

if(argc==3) 
{
    strcpy(source, argv[1]);
    strcpy(target, argv[2]);
}

Also, read up on the dangers of strcpy, and consider strncpy.
But using strcpy will get you started at least...

نصائح أخرى

Use int main(int argc, char **argv); And then assign source to argv[1] and target to argv[2]

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top