0

According to embarcadero docs we can use different Format Specifier to output a buffer to the console in my case I have a function that reads from a stream and outputs to the console but sometimes the stream is wide other times it is narrow and I have to know before hand what kind of stream is it to use the correct format specifier %sor %S

    wchar_t res[8192]= {0};
    FILE * fd;
    if ( (fd = _wpopen(command, L"rb")) == NULL)
        return 0;

    wchar_t c;
    int i =0;
    while ( (c= fgetwc(fd)) != WEOF)
    {
        res[i] = c;
        i++;
    }
    _pclose(fd);
    wprintf(L"%s", res);

Is there a way to know before hand if I should use %sor %S if not how do I ensure that it will always output the result properly

loaded_dypper
  • 262
  • 3
  • 12
  • @Fe2O3 can you elaborate on what you mean by Case and p.u.n.c.t.u.a.t.i.o.n – loaded_dypper Sep 26 '22 at 08:21
  • It is not clear what you are asking. Your program is opening a file. Does it not know what type of contents that file has? If not, how do you expect to discern what type the contents are intended to be? Is it indicated in some metadata? Are you going to examine the file contents for clues? Will the user tell you? What do you mean by “beforehand”? You cannot know when you are writing the program what the contents of some file that does not exist yet will be. – Eric Postpischil Sep 26 '22 at 10:10
  • @EricPostpischil i didn't mention that i am reading from a file and the function i am using is `_wpopen` which creates a pipe and the stream is unicode since the command is `c:\\windows\\system32\\cmd.exe /u /c ` – loaded_dypper Sep 26 '22 at 10:21

1 Answers1

1

I have to know before hand what kind of stream is it to use the correct format specifier

C lib has int fwide(FILE *stream, int mode); to set/report the orientation. Use fwide(stream, 0) to report.

The fwide function returns a value greater than zero if, after the call, the stream has wide orientation, a value less than zero if the stream has byte orientation, or zero if the stream has no orientation.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256