LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ dilemma (https://www.linuxquestions.org/questions/programming-9/c-dilemma-533296/)

Dave256000 02-28-2007 11:36 AM

C++ dilemma
 
Using the skeleton below

#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.

Dave

osor 02-28-2007 12:32 PM

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?

nadroj 02-28-2007 12:44 PM

Quote:

I've been trying for aages and any help or advice would be greatly appreciated.
can you post what you attempted?

graemef 02-28-2007 11:10 PM

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.

Dave256000 03-01-2007 04:49 AM

still no joy
 
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?

Someone, throw me a bone here!

Dave

tread 03-01-2007 06:56 AM

Quote:

Originally Posted by osor
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?

There's the bone.
Here's another: http://www.die.net/doc/linux/man/man2/unlink.2.html

Quote:

Originally Posted by nadroj
can you post what you attempted?


BlueSloth 03-01-2007 05:45 PM

Quote:

Originally Posted by Dave256000
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.

BlueSloth

Dave256000 03-02-2007 02:10 AM

still stuck
 
ok,

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 Headacher 03-02-2007 02:46 AM

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.

This page has some interesting tutorials for both 'c' and 'c++' http://www.cprogramming.com/tutorial.html

File I/O is described here: http://www.cprogramming.com/tutorial/lesson10.html

osor 03-02-2007 08:06 PM

Quote:

Originally Posted by Dave256000
ok,

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.

matthewg42 03-02-2007 08:37 PM

Quote:

Originally Posted by Dave256000
ok,

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:
  1. The first item in the argv array is the program name itself, as typed on the command line
  2. If you want to use C++ style IO, you'll be using cout for output (header is iostream)
  3. 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)
  4. 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


All times are GMT -5. The time now is 02:14 PM.