0

I´m trying to mod a little Program to write text from an Input window to textfile. I´m not very familiar with C and tested a little bit around.

void wr_wprintf(wr_window_t *window, wi_string_t *fmt, ...) {
        wi_string_t             *string;
        va_list                 ap;

        va_start(ap, fmt);
        string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap);
        va_end(ap);
        wr_wprint(window, string);

        FILE *fp;
        fp = fopen("/home/pi/test", "w");
        fprintf(fp, string);
        fclose(fp);

        wi_release(string);
}

The command wr_wprint(window, string); is the output of the text. As you can see I tried already something with fopen. But it did not write the text to file. The file is created but there is nothing inside.

Sascha7777
  • 89
  • 8

1 Answers1

0

UPDATE

After getting access to the library repo being used, I found the definition of wi_string_t. It is part of the libwired submodule. From there, the definition of wi_string_t can be found in libwired/data/wi-string.c. The fact that it is defined inside a c file (traditionally) means your program does not know the contents of the struct. This means that wi_string_t* acts as an "opaque pointer". Since we must pretend not to know the contents of the struct, we must utilize the API functions defined in wi-string.h. I was able to find the following in there:

 93 WI_EXPORT wi_uinteger_t                     wi_string_length(wi_string_t *);
 94 WI_EXPORT const char *                      wi_string_cstring(wi_string_t *);
 95 WI_EXPORT char                              wi_string_character_at_index(wi_string_t *, wi_uinteger_t);

At line 94, we have the function wi_string_cstring. A function that returns a const char* to the data itself. This is exactly what we need.

The following should work for you, but I didn't compile the library to know for sure:

void wr_wprintf(wr_window_t *window, wi_string_t *fmt, ...) {
        wi_string_t             *string;
        va_list                 ap;

        va_start(ap, fmt);
       

        string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap);
        va_end(ap);
        wr_wprint(window, string);

        FILE *fp;
        fp = fopen("/home/pi/test", "w");
        if (fp == NULL) {
                // error handling
        } else {
                fputs(wi_string_cstring(string), fp);
                fclose(fp);
        }

        wi_release(string);
}
Jason
  • 2,493
  • 2
  • 27
  • 27
  • 1
    I suppose he should also use `"a"` instead of `"w"` – Jabberwocky Jan 04 '23 at 14:45
  • @Jabberwocky You are probably right about that. – Jason Jan 04 '23 at 14:47
  • 1
    I suppose this can only work if `wi_string_t` is actually a `char` or somethign similar. – Jabberwocky Jan 04 '23 at 14:47
  • @Jabberwocky. Oh shit... I assumed the original `fmt` was. – Jason Jan 04 '23 at 14:49
  • 1
    Unfortunately that does not work. @Jason The code is here. Line 409 https://github.com/ProfDrLuigi/wire/blob/master/wire/windows.c – Sascha7777 Jan 05 '23 at 14:09
  • @Jason Thanks. Now it compiles without warning. And there is now something in the output file. But only a %@ and not the entered text. – Sascha7777 Jan 05 '23 at 19:09
  • @Sascha7777 That sounds like it has more to do with how you are calling this function than this function itself. Could you post how this function is called? I'm curious how `fmt` is initialized. – Jason Jan 05 '23 at 19:37
  • @Jason The problem is I don´t know nearly nothing about C and try very "bloody" to fiddling around with trial and error. In the running program the function ist called this way: https://i.imgur.com/2yMCzbG.png But that is no help for you I think. – Sascha7777 Jan 05 '23 at 19:42
  • @Sascha7777 It looks like they are not using a standard printf style. I just noticed that when you said it gave you `%@`... see updated code block for an approach that uses the string that `wi_string_init_with_format_and_arguments` builds. – Jason Jan 05 '23 at 20:29
  • Now I got a Memoryaccess Violation on running – Sascha7777 Jan 05 '23 at 20:36
  • 1
    @Sascha7777 I switched the arguments in `fputs`. Try again. – Jason Jan 05 '23 at 21:28
  • Yeah, that did the trick. :D Big thanks. Perhaps you can have a look on another problem which I posted? Thats the Server Side: https://stackoverflow.com/questions/75023525/homebrew-client-server-system-is-exiting-with-stack-smashing-detected-error-on – Sascha7777 Jan 05 '23 at 22:01