LinuxQuestions.org
Review your favorite Linux distribution.
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-25-2005, 12:12 PM   #1
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Rep: Reputation: 15
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);
}
 
Old 02-25-2005, 12:29 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 02-25-2005, 12:30 PM   #3
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
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);
}
 
Old 02-25-2005, 12:37 PM   #4
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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).
 
Old 02-25-2005, 03:12 PM   #5
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
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?
 
Old 02-25-2005, 03:14 PM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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...
 
Old 02-26-2005, 10:37 AM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
If its a homework question then we wont help you


Please read the requirements for this forem
 
Old 02-26-2005, 11:03 AM   #8
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
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.
 
Old 02-27-2005, 12:08 AM   #9
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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)
 
  


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
what cd iso copying/burning program should i use??? nenyo Linux - Software 2 08-24-2005 07:14 PM
copying a file enrique_arong Linux - Newbie 5 07-29-2004 12:21 PM
Question on File System and File Copying hey_joe Linux - Newbie 2 11-05-2003 10:30 AM
copying file ronss Linux - Software 3 08-31-2003 03:04 AM
Copying a file tgonetwork Linux - Newbie 3 01-16-2002 10:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:11 AM.

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
Open Source Consulting | Domain Registration