ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <unistd.h> // read/write
#include <sys/file.h> // open/close values
#include <string.h> // strlen
int main( int argc, char *argv[], char *env[] )
{
// C++ or C code
}
Write a C++ application myrm that removes (deletes) files passed as command line
argument. Use only the Unix/Linux API in your program, do not use standard library
functions.
echo > File1
./myrm File1
I've been trying for aages and any help or advice would be greatly appreciated.
I don’t understand why you can’t use the standard c library functions (e.g., unlink() or remove()) which happen to be part of the Unix API. Is this homework?
The clue that you need is in the skeleton code that you posted. You are obviously required to use functions that are in (and are only in) the #includes. Look at these files, see what is available and use those that help you answer the problem.
Thankyou for your replies, but don't forget somewhere in there I have to write an application myrm that deletes the given file. So that already gives away that I have to use the rm command.
I just can't get my head around how I fit that into the skeleton provided. Can I pass files to the rm command as a parameter? and if so, why is the skeleton structured like that?
Also, how do I call an application like that from that c++ skeleton?
I don’t understand why you can’t use the standard c library functions (e.g., unlink() or remove()) which happen to be part of the Unix API. Is this homework?
Thankyou for your replies, but don't forget somewhere in there I have to write an application myrm that deletes the given file. So that already gives away that I have to use the rm command.
I'm pretty sure the point of the exercise is to implement your own rm, not to use the rm that's already there.
If you are completely new to 'c' I recommend getting yourself a book about it. I have one and it helps me when I get confused about the syntax. Also, you learn stuff in the 'correct' order.
so say I write an application that uses the unlink() command. how would i pass a parameter (i.e. file) to that
for example if
/.myrm file1
removes file1. whats inside myrm that's passing the parameter to the unlink() command?
As you can tell, I'm completely new to this and appreciate the help.
Now if you read the manpage I linked to (from the POSIX specification), you’ll see that unlink takes as a sole argument a c-style string containing the path of the file to remove (e.g., to remove file1, you do “unlink("file1");”). Now, all you need to figure out is how to use command-line arguments.
so say I write an application that uses the unlink() command. how would i pass a parameter (i.e. file) to that
for example if
/.myrm file1
removes file1. whats inside myrm that's passing the parameter to the unlink() command?
As you can tell, I'm completely new to this and appreciate the help.
The arguments to the main function, argc and argv, are the mechanism for passing command line arguments to your program - argc being the number of arguments, argv being an array of pointers to the first character of ach argument.
A good first test program for you to make would be to work out how to make a program called "test1", which, when invoked like this:
Code:
./test1 argument1 second_argument 3rd-arg
...would generate this output:
Code:
argument 1 is: argument1
argument 2 is: second_argument
argument 3 is: 3rd-arg
Hints:
The first item in the argv array is the program name itself, as typed on the command line
If you want to use C++ style IO, you'll be using cout for output (header is iostream)
Old-school C style will use printf for output (header is stdio.h, or, more correctly if you're compiling with a C++ compiler, cstdio)
Don't rest till you fully understand what argv is. Pointers in C are confusing to start with, and take some work to get right in your head
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.