Cannot write with sysfile_write or sysfile_writetextfile

Emerson's icon

Hi, I'm trying to write a file using the routine in the documentation, and the external builds ok, but when it is sent the filename, though it creates a file with that name, the file is always containing zero bytes. It cannot write the data/text in the created file. (Btw, when I use sysfile_write, it crashes Max. I even tried to resort to general methods with stdio.h: FILE *fp and fopen/fwrite/fclose, but they are ineffective.)

I did these:

/////among prototypes:

void fthing_write(t_fthing *x, t_symbol *s);
void fthing_dowrite(t_fthing *x, t_symbol *s);

/////inside ext_main:

class_addmethod(c, (method)fthing_write, "write", A_DEFSYM, 0);

/////

void fthing_write(t_fthing *x, t_symbol *s)
{
defer(x, (method)fthing_dowrite, s, 0, NULL);
}

void fthing_dowrite(t_fthing *x, t_symbol *s)
{
char filename[MAX_FILENAME_CHARS];
short path = path_getdefault();
char *buf = "write me into a file";
long err;
t_filehandle fh;

strcpy(filename, s->s_name);

err = path_createsysfile(filename, path, 'TEXT', &fh);
if (err)
{
post("could not create file");
return;
}

err = sysfile_writetextfile(fh, &buf, TEXT_LB_NATIVE);
post("%ld", err); //err shows 0.
sysfile_close(fh);
}

Any help to find out what's missing here?

Emerson's icon

Can updating from 8.1.2 to the new 8.1.3 solve this problem? (Using Mojave, not Catalina.)

Emerson's icon

Just did the update, the problem didn't go away.

Emerson's icon

So, any experts around?

Emerson's icon

Solved (for now).

Emerson's icon

To conclude the monologue on this topic, let me mention how I solved it so it may help someone else:

1. Including ext_path.h is not enough; file writing functions also need ext_sysfile.h.

2. In the writing-not-restricted-to-text (fwrite substitute) option, variable "count" given in the example from documentation causes a type mismatch in the function call. Instead of "long", it must be declared as "unsigned long long".

pdelges's icon

Thanks Emerson.