Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

MinGW program

Name: Anonymous 2007-07-03 16:01 ID:QeLHvrNT

I'm porting a small app from linux to windows and I need a replacement for truncate() function

will this:

FILE *file;
char *path="/tmp/aaa.txt";
long outpos;
file = fopen(path);
/* some code changing file */
chsize(file->_file, outpos);
fclose(file);

work as in the same way as truncate()?

Name: Anonymous 2007-07-03 18:32 ID:/gwEoROl

WHAT TEH FUCK IS THIS SHIT
SERIOSULY

file = fopen(path); /* WHAT THE FUCK */

chsize(file->_file, outpos); /* WAIT WHAT */

Name: Anonymous 2007-07-03 20:45 ID:FO6Cozgi

>>2
lol I thouht it said CHINEZE(file->....)

Name: Anonymous 2007-07-03 21:50 ID:oiNTIXRE

>>3
it does

Name: Anonymous 2007-07-04 7:10 ID:8b1G1TJh

So you replace a unistd.h function with another unistd.h function thinking it would be more portable?

Name: Anonymous 2007-07-04 8:44 ID:+9Bg4orM

>>5
since when chsize() is a unistd.h function?

if i'm not clear enough:

this is orginal code:

FILE *file;
char *path="/tmp/aaa.txt";
long outpos;
file = fopen(path);
/* some code changing file */
truncate(path);
fclose(file);

this is new code:

FILE *file;
char *path="/tmp/aaa.txt";
long outpos;
file = fopen(path);
/* some code changing file */
chsize(file->_file, outpos);
fclose(file);

will those two work the same way?

Name: Anonymous 2007-07-04 9:14 ID:Heaven

file = fopen(path);
FAIL.
FILE * fopen(const char * restrict path, const char * restrict mode);

truncate(path);
FAIL.
int truncate(const char *path, off_t length);

also, if you have the file open for writing you can just use ftruncate() on the file descriptor (file) instead of using truncate() on the path.
ftruncate() is in mingw's unistd.h.

Name: Anonymous 2007-07-04 9:43 ID:+9Bg4orM

>>7
as for errors, i was writing from memory, not copypasted it from  program, it should be
file=fopen(path,"r+");
and
truncate(path, outpos);

ftruncate() does exacly the same what chsize() does..

the question is: is _file field in FILE struct the file descriptor which i can use with ftuncate() or chsize()?

Name: Anonymous 2007-07-04 11:26 ID:Heaven

>>8
int fileno(FILE *stream);
...
The function fileno() examines the argument stream and returns its integer descriptor.


file=fopen(path,"r+");
...
ftruncate(fileno(file),outpos);

Name: Anonymous 2007-07-04 13:23 ID:8b1G1TJh

>>8
The FILE structure is to be treated as an abstract object and only to be accessed through functions like in >>9. Everything inside is implementation specific and shouldn't be touched. Doing so reduces your portability severely, potentially limiting the code to only your platform and MinGW version.
From the MinGW stdio.h:

/*
 * The structure underlying the FILE type.
 *
 * Some believe that nobody in their right mind should make use of the
 * internals of this structure. Provided by Pedro A. Aranda Gutiirrez
 * <paag@tid.es>;.
 */

Name: Anonymous 2007-07-04 14:31 ID:+9Bg4orM

>>9
thanks

doing fflush(file) beforehand would be a wise decission, wouldn't be?

Name: Anonymous 2007-07-04 16:51 ID:Heaven

>>10
The OP is porting to WINDOWS, so that doesn't matter.

fh = CreateFile(filename,GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
SetFilePointer(fh,length-1,0,FILE_BEGIN);
SetEndOfFile(fh);
CloseHandle(fh);

Name: Anonymous 2007-07-04 19:52 ID:TYnb6Dem

You won't achieve Satori by working in such low-level.

Abstract that to the language. Just say file.trunc(). Or even better, just say file("name").size = x. Or even better, just say do_what_i_want(). Or even better, don't say it, just think about it.

Name: Anonymous 2007-07-05 5:32 ID:pQS+Vupc

Win32 API is the polar opposite of Satori >_<

Name: Anonymous 2007-07-05 10:55 ID:Heaven

[quote]Win32 API is a popular alternative to Satori[/quote]
fixed ^

Name: Anonymous 2009-01-14 14:49

Win32api is deprecated

Name: Anonymous 2010-12-23 15:11


Don't change these.
Name: Email:
Entire Thread Thread List