iostream

Detecting reason for failure to open an ofstream when fail() is true

…衆ロ難τιáo~ 提交于 2019-11-26 21:48:47
问题 Seems like this should be simple, but I don't find it in a net search. I have an ofstream which is open() , and fail() is now true. I'd like to know the reason for the failure to open, like with errno I would do sys_errlist[errno] . 回答1: Unfortunately, there is no standard way of finding out exactly why open() failed. Note that sys_errlist is not standard C++ (or Standard C, I believe). 回答2: The strerror function from <cstring> might be useful. This isn't necessarily standard or portable, but

Why do I need to include both the iostream and fstream headers to open a file

三世轮回 提交于 2019-11-26 21:12:32
问题 #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open ("test.txt"); return 0; } fstream is derived from iostream, why should we include both in the code above? I removed fstream, however, there is an error with ofstream. My question is ofstream is derived from ostream, why fstream is needed to make it compile? 回答1: You need to include fstream because that's where the definition of the ofstream class is. You've kind of got this backwards: since

Why does std::cout convert volatile pointers to bool?

守給你的承諾、 提交于 2019-11-26 21:04:38
If you try to cout a pointer to a volatile type, even a volatile char pointer where you would normally expect cout to print the string, you will instead simply get '1' (assuming the pointer is not null I think). I assume output stream operator<< is template specialized for volatile pointers, but my question is, why? What use case motivates this behavior? Example code: #include <iostream> #include <cstring> int main() { char x[500]; std::strcpy(x, "Hello world"); int y; int *z = &y; std::cout << x << std::endl; std::cout << (char volatile*)x << std::endl; std::cout << z << std::endl; std::cout

How do I deal with the max macro in windows.h colliding with max in std?

烂漫一生 提交于 2019-11-26 20:59:42
So I was trying to get valid integer input from cin, and used an answer to this question . It recommended: #include <Windows.h> // includes WinDef.h which defines min() max() #include <iostream> using std::cin; using std::cout; void Foo() { int delay = 0; do { if(cin.fail()) { cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } cout << "Enter number of seconds between submissions: "; } while(!(cin >> delay) || delay == 0); } Which gives me an error on Windows, saying that the max macro doesn't take that many arguments. Which means I have to do this do { if(cin.fail())

Inheriting std::istream or equivalent

我怕爱的太早我们不能终老 提交于 2019-11-26 20:57:17
问题 I need to bridge two libraries over a stream. QDataStream which is a stream from Qt and some function from another libraries that looks like this void read_something(istream& i); I have no control over how the QDataStream is created and I'm not allowed to change the interface of read_somthing function. The first thing I can think of is write a class that inherits istream and wraps QDataStream. Have anybody done that before? If what I thought wasn't the proper way, I wonder what is the best

generic way to print out variable name in c++

扶醉桌前 提交于 2019-11-26 20:43:59
问题 given a class struct { int a1; bool a2; ... char* a500; ... char a10000; } I want to print or stream out "a1 value is SOME_VALUE" "a2 value is SOME_VALUE" "a500 value is SOME_VALUE" ... "a10000 value is SOME_VALUE" the type of the member variables are not the same (mainly, int, bool, char*, etc, i.e., no need to overload << operator), and the member variable name could be named with anything, i.e., no rule to follow. Instead of typing explicitely one by one (very big tedious, and error-prone

Standard no-op output stream

北战南征 提交于 2019-11-26 19:56:40
问题 Is there a way to create an ostream instance which basically doesn't do anything ? For example : std::ostream dummyStream(...); dummyStream << "Nothing will be printed"; I could just create an ostringstream, but data will be buffered (and I really don't want to make anything with them, so it adds a useless overhead). Any idea ? [edit] Found this related question which suits my needs. However, I think it could be useful to have a answer saying how to create a valid (no badbit) output stream

Reading a file character by character in C

陌路散爱 提交于 2019-11-26 19:09:54
问题 Hey everyone, I'm writing a BF interpreter in C and I've run into a problem reading files. I used to use scanf in order to read the first string, but then you couldn't have spaces or comments in your BF code. Right now here is what I have. char *readFile(char *fileName) { FILE *file; char *code = malloc(1000 * sizeof(char)); file = fopen(fileName, "r"); do { *code++ = (char)fgetc(file); } while(*code != EOF); return code; } I know the problem arises in how I'm assigning the next char in the

Can I use CreateFile, but force the handle into a std::ofstream?

依然范特西╮ 提交于 2019-11-26 18:58:43
Is there any way to take advantage of the file creation flags in the Win32 API such as FILE_FLAG_DELETE_ON_CLOSE or FILE_FLAG_WRITE_THROUGH as described here http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx , but then force that handle into a std::ofstream? The interface to ofstream is obviously platform independent; I'd like to force some platform dependent settings in 'under the hood' as it were. It is possible to attach a C++ std::ofstream to a Windows file handle. The following code works in VS2008: HANDLE file_handle = CreateFile( file_name, GENERIC_WRITE, 0, NULL, CREATE

How can I change the precision of printing with the stl?

左心房为你撑大大i 提交于 2019-11-26 18:35:53
问题 I want to print numbers to a file using the stl with the number of decimal places, rather than overall precision. So, if I do this: int precision = 16; std::vector<double> thePoint(3); thePoint[0] = 86.3671436; thePoint[1] = -334.8866574; thePoint[2] = 24.2814; ofstream file1(tempFileName, ios::trunc); file1 << std::setprecision(precision) << thePoint[0] << "\\" << thePoint[1] << "\\" << thePoint[2] << "\\"; I'll get numbers like this: 86.36714359999999\-334.8866574\24.28140258789063 What I