Prev: Generic compare function
Next: reasoning of ::std::cout (ostream) conversion w.r.t. plain, signed and unsigned chars...
From: piwi on 8 Jan 2010 08:26 Hello, I'm looking for implementing a way to indent some data fed to an std::ostream. I found this link: http://stackoverflow.com/questions/1391746/how-to-easily-indent-output-to-ofstream that proposes to use a facet; I think it is a good idea (and having the opportunity to work with locales for the first time), so I'm starting with the provided code from the above link. Let me emphasize that I'm really not confident with characters encoding and such, as it's the first time I take a look at that; so the solution might be obvious, but I could not find it. Anyway, the code compiles, but does not work all the time. More specifically, it indents text when writing into a std::fstream, but not when writing in a std::cout ! That's what bothers me. When I put a breakpoint in the do_out method, it does not go into it. Now, I read in the STL reference that codecvt<char, char> does not perform conversion at all; but the implementation here always returns true for the method do_always_noconv(). I'm guessing maybe this facet is not used when I'm writing text into cout, is it? In this case, how can I know which facet is used? Thanks a lot for your time ! -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Adam Badura on 11 Jan 2010 02:25 > Anyway, the code compiles, but does not work all the time. More > specifically, it indents text when writing into a std::fstream, but > not when writing in a std::cout ! That's what bothers me. When I put a > breakpoint in the do_out method, it does not go into it. Now, I read > in the STL reference that codecvt<char, char> does not perform > conversion at all; but the implementation here always returns true for > the method do_always_noconv(). I'm guessing maybe this facet is not > used when I'm writing text into cout, is it? In this case, how can I > know which facet is used? Have you called "imbue" function on "cout" object? The accepted answer has a line: -------------------- data.imbue(indentLocale); -------------------- which causes "data" stream to use "indentLocale" std::locale object. You have make this call for "cout" object if you want that feature in "cout" stream (in fact you have to make that call for every stream you want to have that feature in). So try using: -------------------- std::cout.imbue(indentLocale); -------------------- before outputting to "cout". If you already do this (or this does not help) than its hard (for me) to advice anything more without your actual code. Adam Badura -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Walter van der Hee on 11 Jan 2010 05:03 On 9 Jan, 02:26, piwi <bruno.lemarch...(a)gmail.com> wrote: > Hello, > > I'm looking for implementing a way to indent some data fed to an > std::ostream. I found this link: > > http://stackoverflow.com/questions/1391746/how-to-easily-indent-outpu... > > that proposes to use a facet; I think it is a good idea (and having > the opportunity to work with locales for the first time), so I'm > starting with the provided code from the above link. > > Let me emphasize that I'm really not confident with characters > encoding and such, as it's the first time I take a look at that; so > the solution might be obvious, but I could not find it. > > Anyway, the code compiles, but does not work all the time. More > specifically, it indents text when writing into a std::fstream, but > not when writing in a std::cout ! That's what bothers me. When I put a > breakpoint in the do_out method, it does not go into it. Now, I read > in the STL reference that codecvt<char, char> does not perform > conversion at all; but the implementation here always returns true for > the method do_always_noconv(). I'm guessing maybe this facet is not > used when I'm writing text into cout, is it? In this case, how can I > know which facet is used? > > Thanks a lot for your time ! The codecvt facet is only used when you use a filestream, the overflow method on the filebuf calls the out method on the facet. If you want to make your facet working with other streams as well, you need to write a 'filtering streambuf' and call your facet from there. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: DeMarcus on 11 Jan 2010 07:16 piwi wrote: > Hello, > > I'm looking for implementing a way to indent some data fed to an > std::ostream. I found this link: > > http://stackoverflow.com/questions/1391746/how-to-easily-indent-output-to-ofstream > > that proposes to use a facet; I think it is a good idea (and having > the opportunity to work with locales for the first time), so I'm > starting with the provided code from the above link. > > Let me emphasize that I'm really not confident with characters > encoding and such, as it's the first time I take a look at that; so > the solution might be obvious, but I could not find it. > > Anyway, the code compiles, but does not work all the time. More > specifically, it indents text when writing into a std::fstream, but > not when writing in a std::cout ! That's what bothers me. When I put a > breakpoint in the do_out method, it does not go into it. Now, I read > in the STL reference that codecvt<char, char> does not perform > conversion at all; but the implementation here always returns true for > the method do_always_noconv(). I'm guessing maybe this facet is not > used when I'm writing text into cout, is it? In this case, how can I > know which facet is used? > > Thanks a lot for your time ! > This may be what you are looking for. Put it in the beginning of main. std::ios::sync_with_stdio( false ); Regards, Daniel -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: piwi on 12 Jan 2010 01:29
On Jan 12, 1:16 am, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote: > This may be what you are looking for. Put it in the beginning of main. > > std::ios::sync_with_stdio( false ); This indeed solves my problem! But I don't really understand what it does. On the cplusplus.com page: http://www.cplusplus.com/reference/iostream/ios_base/sync_with_stdio/ It says that it "Toggles on or off synchronization of the iostream standard streams with the standard C streams." Do you have any explanation of why turning synch off does use the facet? Thanks, -- Bruno [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |