Implementing Chmod command using c

S

Shastri

Hi all,

I was trying to implement chmod command(UNIX) in c. While passing
arguments to the main function like :

-r for reading the file
-w for writing the file


How can I compare the second argument i.e argv[1] with something like
-r?
While running the program if I enter -r it should read the file.
So if(argv[1]=='-r') can't be done as we cant compare a pointer to an
integer. so Please suggest how to do this??


Thanks
Shastri
 
P

Peter Nilsson

Shastri said:
... While passing arguments to the main function like :

-r for reading the file
-w for writing the file


How can I compare the second argument i.e argv[1] with something like
-r?

if (strcmp(argv[1], "-r") == 0)
...
 
I

Ian Collins

Shastri said:
Hi all,

I was trying to implement chmod command(UNIX) in c. While passing
arguments to the main function like :

-r for reading the file
-w for writing the file


How can I compare the second argument i.e argv[1] with something like
-r?
While running the program if I enter -r it should read the file.
So if(argv[1]=='-r') can't be done as we cant compare a pointer to an
integer. so Please suggest how to do this??
Check for '-', then test the following character.
 
S

sathyashrayan

Shastri said:
Hi all,

I was trying to implement chmod command(UNIX) in c. While passing
arguments to the main function like :

-r for reading the file
-w for writing the file


How can I compare the second argument i.e argv[1] with something like
-r?
While running the program if I enter -r it should read the file.
So if(argv[1]=='-r') can't be done as we cant compare a pointer to an
integer. so Please suggest how to do this??


Thanks
Shastri


int main(int argc, char *argv[])

In a main() like the above you can use multi dimentation array
such as ,argv[rows][coloums] along with counter argc inside a if statement.
Simply use loop iterations for rows and coloums to march through the array.
Hope that helps.
 
B

Barry Schwarz

Hi all,

I was trying to implement chmod command(UNIX) in c. While passing
arguments to the main function like :

-r for reading the file
-w for writing the file


How can I compare the second argument i.e argv[1] with something like
-r?
While running the program if I enter -r it should read the file.
So if(argv[1]=='-r') can't be done as we cant compare a pointer to an
integer. so Please suggest how to do this??

You are looking for the strcmp function.

BTW, '-r' is a no-no. The ' delimiter is for a character constant
containing a single character. You want "-r" which defines a string
literal.
Thanks
Shastri
Remove del for email
 
M

Mark B

Barry Schwarz said:
Hi all,

I was trying to implement chmod command(UNIX) in c. While passing
arguments to the main function like :

-r for reading the file
-w for writing the file


How can I compare the second argument i.e argv[1] with something like
-r?
While running the program if I enter -r it should read the file.
So if(argv[1]=='-r') can't be done as we cant compare a pointer to an
integer. so Please suggest how to do this??

You are looking for the strcmp function.

BTW, '-r' is a no-no. The ' delimiter is for a character constant
containing a single character. You want "-r" which defines a string
literal.

Yeah, I don't think that will work too well either as written...
How about: if(strcmp(argv[1], "-r") == 0)
or better yet, use getopt() if it's available on your platform.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top