Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 21st, 2004, 9:08 AM   #1
sys0p
Programmer
 
Join Date: Oct 2004
Posts: 73
Rep Power: 5 sys0p is on a distinguished road
Hi,
Below is a program I am learning from Practical C++ Programming. Below I asked some questions, and I just want to make sure that I am on the right track

Hope someone can help?!

#include <iostream.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifdef __MSDOS__
#include <io.h>
#else
#include <unistd.h>
#endif

const int BUFFER_SIZE = (16 * 1024);

main(int argc, char *argv[])
{
  char buffer[BUFFER_SIZE];
  int in_file;
  int out_file;
  int read_size;

  if(argc != 3) {
  cerr << "Error: Wrong number of arguments\n";
  cerr << "Usage is: copy <from> <to>\n";
  exit(8);
  }

   in_file = open(argv[1], O_RDONLY);
   if(in_file < 0) {
   cerr << "Unable to open " << argv[1] << '\n';
   exit(8);
   }

	out_file = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0666);
	if(out_file < 0) {
	cerr << "Unable to open " << argv[2] << '\n';
	exit(8);
	}

  while(1) {
  read_size = read(in_file, buffer, sizeof(buffer));
  if(read_size == 0) 
  break;

  if(read_size < 0) {
  cerr << "Error: Read error" << '\n';
  exit(8);
  }

  write(out_file, buffer, (unsigned int) read_size);
  }
  close(in_file);
  close(out_file);
  system("PAUSE");
  return(0);
}

So here
 if(argc != 3) {
we are saying that argc must contain 3 arguments which are:

in_file //argc[1]
out_file //argc[2]
read_size //argc[3]

Is that right?

It doesn't seem right because here:

in_file = open(argv[1], O_RDONLY);
   if(in_file < 0) {

We are saying that in_file = open(in_file (! argc[1] !), O_RDONLY)... maybe it is right, because we are calling our function in_file which runs the operation open(in_file, ...) for reading only. Is that right?

The same goes for

out_file = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0666);
	if(out_file < 0) {

Are we also doing this:

out_file = open(out_file, ...) ?

And when we do the if statement:

if(out_file < 0) {

does the if refer to out_file = open(argc[2], .....)?

I hope someone can help me clear up on this. I'm sure I am correct here, but maybe i'm not?

Anyway, cheers for now!
sys0p is offline   Reply With Quote
Old Oct 21st, 2004, 11:23 AM   #2
mici
Programmer
 
Join Date: Oct 2004
Posts: 67
Rep Power: 5 mici is on a distinguished road
Quote:

we are saying that argc must contain 3 arguments which are:

in_file //argc[1]
out_file //argc[2]
read_size //argc[3]

Is that right?
Nope,
the 3 arguments are
argv[0] => "the name of your app"
argv[1] => "the name of your input file"
argv[2] => "the name of your output file"
the read_size is a variable, and if you mean the number of bytes that you read from the file... then you can see it has been defined in the begining...

const int BUFFER_SIZE = (16 * 1024);

if you dont write anything in the shell except your program name the argc will be 1 not 0.
therefore if you placed the buffer_size after the name of the output file in the shell you would get that argc is 4...
in_file and out_file are just variables of type int. that recieve return values of the function called open() wich is the ID of the file being opened...
in_file = open(argv[1], O_RDONLY);
this way you can hanlde integers in your program instead of filenames...
read_size recieves the return value of the read command... wich is the number of bytes system managed to read from file... it's a simple check if you reached the EndOfFile...



Quote:

because we are calling our function in_file which runs the operation open(in_file, ...) for reading only. Is that right?
in_file is not a function...it's a variable that recieves the output value of the open() wich is a function wich takes in parameters as follows:

FileID = open( "FileName", "Options", "Mode");

where

FileName - is the name of the file you want to open (in your case it's held in the argv[1] for the in_file and in argv[2] for the out_file)

Options - read, write, append, create, truncate (basicly what you want to do with the file once you open it and how you want to open it) etc...

Mode - usefull only when creating or modifying files... tells the system who will be able to read , write, execute the file latter on... (set the attributes for the file).

then the open function returns a value wich is passed to the FileID variable...

FileID - is the unique number asigned to a file that has been opened.

on the other hand read() function expects that you will pass it the ID of an open file and not the file name thus:
read(in_file, ... and not read(argv[1], ...
the same works with write...



Quote:

if(out_file < 0) {
if - here just checks if the open function succeded in the file open procedure... if it didn't it would return a -1 ... thus the variable in_file would contain the value -1 and the program would break execution displaying the message in the {} after the if...
However if the open function succeds it will return a positive number and execution will skip the break;
__________________
coffee is my heroin.
mici is offline   Reply With Quote
Old Oct 22nd, 2004, 9:29 AM   #3
sys0p
Programmer
 
Join Date: Oct 2004
Posts: 73
Rep Power: 5 sys0p is on a distinguished road
Hey!Thanks for the reply!
Of course! What was I thinking:

It should start with the program name!

argc[0] = "copy.exe"
argc[1] = "in_file"
argc[2] = "out_file"

Yeah I notice the read_file is a variable.

Quote:

if you dont write anything in the shell except your program name the argc will be 1 not 0.
Sorry but shouldn't argc[0] always be for the program?

Quote:

therefore if you placed the buffer_size after the name of the output file in the shell you would get that argc is 4...
Ok I understand that.

Quote:

in_file and out_file are just variables of type int. that recieve return values of the function called open() wich is the ID of the file being opened...
Of course! How stupid of me! Yeah in_file and out_file are variables that have been assigned the function open(...).
Silly of me!

Quote:

in_file = open(argv[1], O_RDONLY);
this way you can hanlde integers in your program instead of filenames...
Sorry i'm not sure i'm getting this properly, could it be because in_file, instead of entering the file name you can enter an interger value instead? Is that right?

Meaning that for the if statements you can use the number.... wait i'm definately not getting it Sorry, just had a very stressful day, since I wrote my final accounting exam today, so it's left me with a headache!

Quote:

read_size recieves the return value of the read command... wich is the number of bytes system managed to read from file... it's a simple check if you reached the EndOfFile...
OK I see this on. If here:

	read(in_file, buffer, sizeof(buffer));

Can in_file be substituted with argc[1]? Also can buffer be argc[4], sizeof(buffer) be argc[5]? I should test this bit too

Quote:

FileName - is the name of the file you want to open (in your case it's held in the argv[1] for the in_file and in argv[2] for the out_file)
So argc[1] can be typed as in_file and so on?

Quote:

then the open function returns a value wich is passed to the FileID variable...

FileID - is the unique number asigned to a file that has been opened.

on the other hand read() function expects that you will pass it the ID of an open file and not the file name thus:
read(in_file, ... and not read(argv[1], ...
the same works with write...
Sorry I don't quite get this Could you explain a bit clearer please? I'll try first, do you mean that the read(..) function expects the file to be open to be able to pass itself to the ID of the file? So it first needs to open the file to be able to do this?

Quote:

if - here just checks if the open function succeded in the file open procedure... if it didn't it would return a -1 ... thus the variable in_file would contain the value -1 and the program would break execution displaying the message in the {} after the if...
However if the open function succeds it will return a positive number and execution will skip the break;
Ok cool I understand! Thanks man!

I really appreciate your help
Cheers
sys0p is offline   Reply With Quote
Old Oct 23rd, 2004, 2:19 PM   #4
mici
Programmer
 
Join Date: Oct 2004
Posts: 67
Rep Power: 5 mici is on a distinguished road
Whole lot of your questions are based around the same principal so instead of answering them one by one i'll try to explain the principle it self...

When ever you declare a function in C you do it like this

type name(argument type list);

where the first "type" describes the type of the value wich is returned by the function...

"argument type list" contains the list of types of the arguments you will pass to the function.
i.e.
"int myfunc(double, int, char)"
from this i can see that the function takes 3 parameters ... 1st of the type double, 2nd type integer and the 3rd one is a character.
i can also see that this function called "myfunc" returns an integer value...

so whatever is written in this function, somewhere near the last } there is a
"return value" where value is an integer value. (it can be a variable containing a number of type int or just a number)
i.e. let's look at this small function...

int myfunc( int value)
{
return value;
}

--it's completly pointles because all it does is return the same value that you pass to it...
Just like myfunc, main() is a simple function...
what's special about main() is that it's allways the first function that executes. As such it should be able to recieve arguments (parameters) just like any other function... To give this ability to the main() ppl have devised the infamous argc and argv[]
argc - stands for argument count... and contains the exact number of strings you've typed in the command line (or in the shell)...
the strings are defined (in this case) as arrays of characters sepparated from each side by white spaces...
now since you have to type your program name to be able to run it minimal value for argc is 1, not 0.
and since all arrays in C start with offset 0 it can be said that argc =1 equals to argv[0]...

argv[0] stands for argument vector.
it's called vector because it's not predefined in size... meaning it can contain as many values, strings as necessary...

every function in C returns a value except those defined as void type...(theese can just do return; vithout adding a value)

i've got to go....
to be continued...
__________________
coffee is my heroin.
mici is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC