string question

B

Bill Cunningham

void chunk(char *file1, char *file2, size_t bytes)
Do I need to add another parameter to this to use as a buffer?
maybe I should add a fourth parameter, the generic pointer type void*.
That's what fread's first parameter is. I do need a storage space don't I.
The 4th parameter should probably be the same as the 3rd in size. Still a
bit confused.

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
maybe I should add a fourth parameter, the generic pointer type void*.
That's what fread's first parameter is. I do need a storage space don't I.
The 4th parameter should probably be the same as the 3rd in size. Still a
bit confused.

You don't need another parameter. You need a plan of how you are
going to do the copy and then you can code it up.

One way to copy N bytes from one file to another is simply one at a
time. Use fgetc to read a character and fputc to write it. One thing
to watch: you need to store the character in a int because one of the
possible return values is EOF which can only be properly represented
in an int.

You can copy the bytes by reading an the writing larger pieces, but
this is a bit more complex. I'd suggest you try one byte at a time
first.
 
J

James Kuyper

Bill Cunningham wrote:
....
Oh I see. So would it be better to change char file1 [] to char *file1 ?
I don't know how to declar a size_t yet or use sizeof to retrieve it in this
case.

I regularly use a compiler which, in maximum warning mode, will warn you
about a parameter declared as char *file1 if your code never changes the
value of that pointer, saying that you should declare the pointer as
"char * const file1". This warning does not occur if the declaration
says char file[]. In C++, this would be a slightly more useful warning,
but in C its pretty much pointless.

Incidentally, if you never write through the pointer, the compiler does
NOT suggest that you declare the pointed "char const * file1", which
would have been a much more useful warning.

However, with the exception of one ditzy compiler, there's no good
reason to favor one form over the other. Use whichever form feels more
appropriate. I use * when pointing at a single object, and [] when
pointing at the first element of an array of objects.
 
D

Default User

Bill Cunningham wrote:

I'm still not clear of fread. I've always used it like this in the
past fread (buf name,sizeof(char),512,fp); But how to pass parameters
from a function fread is used in confuses me. If size_t is there I
might as well use it.

Certainly if that is specified in the interface it should be used.
void chunk(char *file1, char *file2, size_t bytes)

Ah, looks like you intend to copy from one file to another, not
retrieve a specific size chunk of data. Is that correct?
Do I need to add another parameter to this to use as a buffer?

Probably not, but I'm not certain. At this point, you should write down
in text form what exactly chunk() is supposed to do. That will guide
you to the implementation needed to achieve that goal.





Brian
 
B

Bill Cunningham

Default User said:
Certainly if that is specified in the interface it should be used.


Ah, looks like you intend to copy from one file to another, not
retrieve a specific size chunk of data. Is that correct?


Probably not, but I'm not certain. At this point, you should write down
in text form what exactly chunk() is supposed to do. That will guide
you to the implementation needed to achieve that goal.

I want to copy from the front of a file so many bytes and write it to a
file. Like this linux and unix program does,

dd if=file.ext of=file2.exe bs=2048(or 512) count=1

The 3rd parameter does what bs does in dd. The 1st parameter of chunk would
do what if= does and the 2nd what of= does.

Bill
 
D

Default User

Bill said:
I want to copy from the front of a file so many bytes and write it
to a file.

All right.
Like this linux and unix program does,

dd if=file.ext of=file2.exe bs=2048(or 512) count=1

The 3rd parameter does what bs does in dd. The 1st parameter of chunk
would do what if= does and the 2nd what of= does.

Ok. What "bs" is here is block size. That means, how big of chuncks
will you read at a time, with "count" being then number of blocks to
transfer. dd in its basic form copies the entire file unless you give
it a count.


So to give you what dd does (or part of it rather), start with a
signature like:

int chunk(const char *infile, const char *outfile, size_t blocksize,
size_t blockcount);

I've made the parameters more descriptive than dd or your original. You
also want a return value to let the caller know whether it went ok or
not.

Do you understand what the purpose of each parameter is, and what you
will do with them in the function?




Brian
 
K

Kelsey Bjarnason

Ben Bacarisse said:
Malcolm McLean said:
You forgot to cast the value to int. You're trying to print a
size_t value with a "%d" format. It might happen to work, or it
might not.

Ah! Thanks. I did not want to use %zu, but then forgot the cast.
[Note to self: post no code after 1am.]

I'll offer you another chance to order a "give me 64" T-shirt.

Not all gone yet?

Do you have one that says:

The only place you really need a cast in C is printing size_t in
C90... and passing a char ** as a const char *const *. No, the *two*
places you need a cast are: printing size_t in C90, passing a char **
as a const char *const * and when calling ctype.h functions.
No, the *three* places... *Amongst* the places you need a cast are
printing size_t in C90...
Too busy.

Try
Let him who has 64 bits cast the first size_t.

Don't see how 64 bits enters into it; you're either using the right type
or you're not, and if you're not, you may have to cast. Or, better yet,
learn how to write code so you don't need to cast, except the rare cases
where it's unavoidable.
 
B

Bill Cunningham

Default User said:
Ok. What "bs" is here is block size. That means, how big of chuncks
will you read at a time, with "count" being then number of blocks to
transfer. dd in its basic form copies the entire file unless you give
it a count.


So to give you what dd does (or part of it rather), start with a
signature like:

int chunk(const char *infile, const char *outfile, size_t blocksize,
size_t blockcount); [snip]

Do you understand what the purpose of each parameter is, and what you
will do with them in the function?

It looks like the first 3 would do what the first 3 in my function would do
but I don't know the purpose of the 4th parameter. I'm not quite sure why
the first 2 parameters are constants but I don't see why it would hurt
anything.

What is size_t blockcount for?

Bill
 
D

Default User

Bill said:
"Default User" <[email protected]> wrote in message
So to give you what dd does (or part of it rather), start with a
signature like:

int chunk(const char *infile, const char *outfile, size_t blocksize,
size_t blockcount); [snip]

Do you understand what the purpose of each parameter is, and what
you will do with them in the function?

It looks like the first 3 would do what the first 3 in my function
would do

But what is that functionality specifically. Write out what you think
the purpose of them is. From the prior messages, I'm not sure you are
clear on what your own proposed function would use its parameters for.
but I don't know the purpose of the 4th parameter.

Read over the man page for dd. See what options it specifies.
I'm not
quite sure why the first 2 parameters are constants but I don't see
why it would hurt anything.

Why would they NOT be constants? What does declaring a parameter as
const char* mean?
What is size_t blockcount for?

Again, look at dd. Think about what your function would actually be
used for.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top