The only way in plain standard C is to write x bytes to the file after opening it in binary mode, and even then, the standard allows an implementation to append an arbitrary number of null bytes to the end without telling you.

On the other hand, with POSIX, it's easy. Simply call ftruncate(fileno(f), x);

For creating the new file - I'm not sure exactly what you'd be asking. Do you want to create a file that's empty but has a particular size? Why would you need to do that? The sparse file technique that others have described (fseek + fputc) creates a "sparse" file that takes up almost no space on disk, but (if you read it) returns all 0s.

I'd like set the size of a file. much like Adrian suggested I "believe".
–
QuestioneerOct 15 '11 at 1:10

1

Sorry, I still don't get it - when I asked you to clarify what you mean by "how do I set the size of the file" you repeated "I'd like to set the size of the file." Can you clarify what you mean exactly, and describe in more detail what you're doing?
–
uscjeremyOct 15 '11 at 1:17

create a file that's empty but has a particular size. Sorry for the vague response
–
QuestioneerOct 15 '11 at 1:20

2

@Questioneer: What exactly do you mean by "empty"? Normally, a file with a non-zero size is not empty. A file (on typical systems) is a sequence of zero or more bytes, and each of those bytes has a value.
–
Keith ThompsonOct 15 '11 at 1:49

2

I'm still not entirely clear on what you mean by "empty but has a particular size". Do you mean it contains all 0s? Do you mean a sparse file? Do you mean a file whose space has been allocated on disk? We need more context to answer this question sensibly.
–
uscjeremyOct 15 '11 at 1:54