File copying program
I am writing a small program that will:
1. Accept two paramters oldsource and newsource then 2. Create the newsource file 3. Copy the contents from the oldsource file to the newsource file 4. Delete the oldsource file I have everything but the copying part down. Below is a snippet from my program which is intended to be actively reading from oldsource and writing to newsource but something is not working. The file is not being written to. BTW, Arg2=buffer Arg3=Source Arg4=Target opensource is: opensource = open(argv[3],O_CREAT) opentarget is: opentarget = open(argv[4],O_CREAT) Quote:
|
How do you get argv[2] as a buffer?
You also need to open opensource with O_RDONLY flag, and you should open the opentarget with O_WRONLY|O_CREAT|O_TRUNC flags. |
Not sure to solve the problem, but try to remoce lseek part. It's not needed since it increases the file pointer as it writes to it.
Also note that arg[0] will hold the executable file name, so buffer may be arg[1], source may be arg[2] and target may be arg[3]. Code:
while ((count = read(opensource,bufferCount,1)) != EOF) |
ok, quite a few things wrong.... this also sounds like a homework question.
Basically, man -s 2 read man -s 2 write read won't return EOF at the end of the file. read and write automatically "seek" after each read and write the number of characters you read or write (i.e. no need for lseek there...). The lseek won't work the way you want either, it'll keep trying to write at the position "2" in the file since read returns the # of bytes read. For their second option, the buffer to read/write from, I suggest you don't use argv[2]. For the "size" (3rd argument), you can probably use something much larger, i.e. 1024 or 512 or something, then write "count" characters. When your at the end of the file, read will return something less than the number of characters you ask for (i.e. 1024 or 512), in your case, 1 (so at EOF it would return 0). |
Yes, this is a homework question.
Quote:
Oh and this program can create a file with holes, and that is arg[1]. Sorry, forgot to type that earlier. Since read does not return EOF, does this mean that I can just use: while (count = read(opensource,argv[2],512)) { ...do something } If read returns 511 for example, the loop would end correct? |
You're not using argv[2] as the buffer size there, you're using argv[2] as the buffer itself. Bad bad. And what do you mean by holes? Holes at every such and such offset? What does the user specify for argv[1]? You're seriously lacking in the details department here...
|
If its a homework question then we wont help you :)
Please read the requirements for this forem |
Quote:
Quote:
Quote:
exvor don't worry I'm not asking for anyone to do my homework, that wouldn't benefit me other than a good grade. I'm just trying to understand something clearly. |
if you want to use argv[2] as your buffer SIZE then I suggest you read up on sscanf:
man sscanf Your missing the point he was saying, the argument void *buf is the buffer to read to, not the size of the buffer. i.e. you need to pass some address that has enough memory allocated to hold however many bytes your reading. Your while loop probably would work as long as you only write count characters, but it'll cause problems if read returns an error (i.e. -1) |
All times are GMT -5. The time now is 07:24 AM. |