![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2004
Posts: 73
Rep Power: 5
![]() |
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) {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! ![]() |
|
|
|
|
|
#2 | |||
|
Programmer
Join Date: Oct 2004
Posts: 67
Rep Power: 5
![]() |
Quote:
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:
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:
However if the open function succeds it will return a positive number and execution will skip the break;
__________________
coffee is my heroin. |
|||
|
|
|
|
|
#3 | ||||||||
|
Programmer
Join Date: Oct 2004
Posts: 73
Rep Power: 5
![]() |
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:
Quote:
![]() Quote:
Silly of me! Quote:
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(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:
Quote:
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:
I really appreciate your help ![]() Cheers ![]() |
||||||||
|
|
|
|
|
#4 |
|
Programmer
Join Date: Oct 2004
Posts: 67
Rep Power: 5
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|