PDA

View Full Version : changing file size in C/C++



UncleRoy
March 22nd, 2012, 09:08 AM
In Windows one used (int handle) and chsize(handle,newsize) to either truncate or extend the size of a file.
Now in Ubuntu / Unix one uses (FILE* fp) for most IO. What is the equivalent FILE* function for chsize ?

CynicRus
March 22nd, 2012, 10:13 AM
#include <unistd.h>
#include <sys/types.h> int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);

Bachstelze
March 22nd, 2012, 11:25 AM
Note that if you want to use ftruncate(), you must open the file with open(), not fopen(). (If the file is already opened with fopen(), you can use fileno() to get the file descriptor associated with it without open()ing it again.)

UncleRoy
March 22nd, 2012, 01:13 PM
Many thanks. It worked with open (which i didn't know was available in ubuntu and i prefer anyway).
But , please (out of interest) what is the exact syntax for getting a handle(fd) out of a FILE*(fp)

File* fp = fOpen (... etc )
int fd = fp->fileno(); // ?? compiler error

Bachstelze
March 22nd, 2012, 01:22 PM
FILE *fp = fopen(...);
int fd = fileno(fp);

UncleRoy
March 22nd, 2012, 02:08 PM
Touche. Clearly that is the last word.

ofnuts
March 22nd, 2012, 03:33 PM
Note that if you want to use ftruncate(), you must open the file with open(), not fopen(). (If the file is already opened with fopen(), you can use fileno() to get the file descriptor associated with it without open()ing it again.)
Wouldn't that be dangerous to truncate the file unbeknownst to the code that uses the fopen()'ed descriptor?

Bachstelze
March 22nd, 2012, 03:36 PM
Wouldn't that be dangerous to truncate the file unbeknownst to the code that uses the fopen()'ed descriptor?

I can't see why, unless the program is multithreaded and one thread truncates the file while the other is in the middle of a fread/fwrite operation maybe. I suspect that a subsequent call to fread or fwrite would simply behave as it normally does when end-of-file is reached.

ofnuts
March 22nd, 2012, 11:09 PM
I can't see why, unless the program is multithreaded and one thread truncates the file while the other is in the middle of a fread/fwrite operation maybe. I suspect that a subsequent call to fread or fwrite would simply behave as it normally does when end-of-file is reached.My question is really if


char *abcdef="abcdef";
char *ghijkl="ghijkl";

FILE* f=fopen("data","w");
fwrite(abcdef,1,strlen(abcdef),f);
// fflush(f);
ftruncate(fileno(f),3))
fwrite(ghijkl,1,strlen(ghijkl),f);
fclose(f);

creates a file with "abcdefghijkl", "abc___ghijkl" (with whatever (all zeroes?) in the "_") , or "abcghijkl". It turns out that it creates "abcdefghijkl" without the fflush(), and "abcghijkl" with the fflush() . So it's not exactly the problem I expected (write pointer past the current end of file) but truncating an fopen()ed file still requires some care.