iostream

How to simulate printf's %p format when using std::cout?

百般思念 提交于 2019-11-27 15:28:06
unsigned char *teta = ....; ... printf("data at %p\n", teta); // prints 0xXXXXXXXX How can I print variable address using iostream s? Is there a std:: ??? feature like std::hex to do this kind of conversion (address -> string), so std::cout << std::??? << teta << std::endl will print that address? (no sprintf's, please ;)) Cast to void* : unsigned char* teta = ....; std::cout << "data at " << static_cast<void*>(teta) << "\n"; iostreams generally assume you have a string with any char* pointer, but a void* pointer is just that - an address (simplified), so the iostreams can't do anything other

Alternative function in iostream.h for getch() of conio.h?

坚强是说给别人听的谎言 提交于 2019-11-27 14:59:11
I'm trying to hold the screen on my output using the header file <iostream.h> , but I don't know any equivalent function to the getch() & clrscr() functions of <conio.h> in <iostream.h> or any other C++ library. Are there any such functions? if you work on windows you can use system("pause"), this will give you "press any key to continue" message. Stephen Veiss The conio.h functions are compiler extensions to the language, not part of C or C++. There isn't a direct replacement in standard C++. For getch(), int ch = std::cin.get(); is probably the closest equivalent -- but bear in mind that

Using char16_t and char32_t in I/O

回眸只為那壹抹淺笑 提交于 2019-11-27 14:20:14
C++11 introduces char16_t and char32_t to facilitate working with UTF-16- and UTF-32-encoded text strings. But the <iostream> library still only supports the implementation-defined wchar_t for multi-byte I/O. Why has support for char16_t and char32_t not been added to the <iostream> library to complement the wchar_t support? In the proposal Minimal Unicode support for the standard library (revision 2) it is indicated that there was only support among the Library Working Group for supporting the new character types in strings and codecvt facets. Apparently the majority was opposed to supporing

How can I set the decimal separator to be a comma?

不问归期 提交于 2019-11-27 14:09:12
I would like to read and write pi as 3,141592 instead of 3.141592 , as using the comma is common in many European countries. How can I accomplish this with iostream s? In other words cout << 3.141592; should print 3,141592 to standard output. You should use basic_ios::imbue to set the preferred locale. Take a look here: http://www.cplusplus.com/reference/ios/ios_base/imbue/ Locales allow you to use the preferred way by the user, so if a computer in Italy uses comma to separate decimal digits, in the US the dot is still used. Using locales is a Good Practice. But if you want to explicitly force

Reading directly from an std::istream into an std::string

廉价感情. 提交于 2019-11-27 13:42:00
Is there anyway to read a known number of bytes, directly into an std::string, without creating a temporary buffer to do so? eg currently I can do it by boost::uint16_t len; is.read((char*)&len, 2); char *tmpStr = new char[len]; is.read(tmpStr, len); std::string str(tmpStr, len); delete[] tmpStr; std::string has a resize function you could use, or a constructor that'll do the same: boost::uint16_t len; is.read((char*)&len, 2); std::string str(len, '\0'); is.read(&str[0], len); This is untested, and I don't know if strings are mandated to have contiguous storage. You could use a combination of

Loading a file into a vector<char>

时光总嘲笑我的痴心妄想 提交于 2019-11-27 13:38:18
I would like to load the contents of a text file into a vector<char> (or into any char input iterator, if that is possible). Currently my code looks like this: std::vector<char> vec; std::ifstream file("test.txt"); assert(file.is_open()); while (!(file.eof() || file.fail())) { char buffer[100]; file.read(buffer, 100); vec.insert(vec.end(), buffer, buffer + file.gcount()); } I do not like the manual use of a buffer (Why 100 chars? Why not 200, or 25 or whatever?), or the large number of lines that this took. The code just seems very ugly and non-C++. Is there a more direct way of doing this? If

Custom stream manipulator for streaming integers in any base

自闭症网瘾萝莉.ら 提交于 2019-11-27 13:20:11
问题 I can make an std::ostream object output integer numbers in hex, for example std::cout << std::hex << 0xabc; //prints `abc`, not the base-10 representation Is there any manipulator that is universal for all bases? Something like std::cout << std::base(4) << 20; //I want this to output 110 If there is one, then I have no further question. If there isn't one, then can I write one? Won't it require me to access private implementation details of std::ostream ? Note that I know I can write a

Reading files larger than 4GB using c++ stl

戏子无情 提交于 2019-11-27 12:49:39
A few weeks back I was using std::ifstream to read in some files and it was failing immediately on open because the file was larger than 4GB. At the time I couldnt find a decent answer as to why it was limited to 32 bit files sizes, so I wrote my own using native OS API. So, my question then: Is there a way to handle files greater than 4GB in size using std::ifstream/std::ostream (IE: standard c++) EDIT: Using the STL implementation from the VC 9 compiler (Visual Studio 2008). EDIT2: Surely there has to be standard way to support file sizes larger than 4GB. Apparently it depends on how off_t

How to print __int128 in g++?

我与影子孤独终老i 提交于 2019-11-27 12:43:41
问题 I am using the GCC built-in type __int128 for a few things in my C++ program, nothing really significant, at least not enough to justify to use BigInt library only for that and, yet, enough to prevent to remove it totally. My problem comes when I run into the printing parts my classes, here is a minimal example: #include <iostream> int main() { __int128 t = 1234567890; std::cout << t << std::endl; return t; } Commenting out the std::cout line will make this code to compile nicely with g++ ,

How can I print 0x0a instead of 0xa using cout?

做~自己de王妃 提交于 2019-11-27 12:33:18
How can I print 0x0a, instead of 0xa using cout? #include <iostream> using std::cout; using std::endl; using std::hex; int main() { cout << hex << showbase << 10 << endl; } This works for me in GCC: #include <iostream> #include <iomanip> using namespace std; int main() { cout << "0x" << setfill('0') << setw(2) << hex << 10 << endl; } If you are getting sick and tired of iostream's formatting quirkiness, give Boost.Format a try. It allows good-old-fashioned, printf-style format specifiers, yet it is type-safe. #include <iostream> #include <boost/format.hpp> int main() { std::cout << boost: