ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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);
}
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);
}
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).
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...
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.
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.