LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-28-2007, 12:36 PM   #1
Dave256000
LQ Newbie
 
Registered: Feb 2007
Posts: 5

Rep: Reputation: 0
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
 
Old 02-28-2007, 01:32 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
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?

Last edited by osor; 02-28-2007 at 01:48 PM.
 
Old 02-28-2007, 01:44 PM   #3
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 59
Quote:
I've been trying for aages and any help or advice would be greatly appreciated.
can you post what you attempted?
 
Old 03-01-2007, 12:10 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 03-01-2007, 05:49 AM   #5
Dave256000
LQ Newbie
 
Registered: Feb 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 03-01-2007, 07:56 AM   #6
tread
LQ Newbie
 
Registered: Feb 2007
Posts: 29

Rep: Reputation: 15
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?
 
Old 03-01-2007, 06:45 PM   #7
BlueSloth
LQ Newbie
 
Registered: Sep 2005
Distribution: Debian sid
Posts: 9

Rep: Reputation: 0
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
 
Old 03-02-2007, 03:10 AM   #8
Dave256000
LQ Newbie
 
Registered: Feb 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 03-02-2007, 03:46 AM   #9
The Headacher
Member
 
Registered: Jan 2007
Distribution: Vector Linux
Posts: 90

Rep: Reputation: 15
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

Last edited by The Headacher; 03-02-2007 at 03:48 AM.
 
Old 03-02-2007, 09:06 PM   #10
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
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.
 
Old 03-02-2007, 09:37 PM   #11
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boot dilemma zatka Linux - Software 2 05-17-2005 09:51 AM
Debian Dilemma habala Linux - Newbie 3 12-31-2004 01:47 PM
dilemma?(perl) akaash Programming 1 04-03-2004 05:06 PM
Slackware dilemma usr Slackware 6 01-30-2004 01:04 PM
CD-R/RW dilemma neo77777 General 10 09-07-2002 10:42 AM


All times are GMT -5. The time now is 08:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration