LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   File copying program (https://www.linuxquestions.org/questions/programming-9/file-copying-program-294791/)

alltime 02-25-2005 12:12 PM

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:

while ((count = read(opensource,argv[2],1)) != EOF)
{
// Write the file content
write(opentarget,argv[2],1);
lseek(opensource,count++,SEEK_SET);
}

itsme86 02-25-2005 12:29 PM

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.

Mohsen 02-25-2005 12:30 PM

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)
{
  write(opentarget,count,1);
}


95se 02-25-2005 12:37 PM

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

alltime 02-25-2005 03:12 PM

Yes, this is a homework question.

Quote:

I suggest you don't use argv[2].
The requirement is that the user must specifiy the buffer size to be used.

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?

itsme86 02-25-2005 03:14 PM

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

exvor 02-26-2005 10:37 AM

If its a homework question then we wont help you :)


Please read the requirements for this forem

alltime 02-26-2005 11:03 AM

Quote:

You're not using argv[2] as the buffer size there, you're using argv[2] as the buffer itself. Bad bad.
Would copying arg[2] to my own local variable be a better idea?

Quote:

And what do you mean by holes? Holes at every such and such offset?
Yes

Quote:

What does the user specify for argv[1]? You're seriously lacking in the details department here...
Sorry, arg[1] is just the user passing -h which the program checks in a loop. If -h is specified, I copy the file but include holes within the file.[/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.

95se 02-27-2005 12:08 AM

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.