iostream

Why is the address of this volatile variable always at 1?

梦想的初衷 提交于 2019-11-27 04:29:44
问题 I wanted to inspect the address of my variable volatile int clock; cout << &clock; But it always says that x is at address 1. Am i doing something wrong?? 回答1: iostreams will cast most pointers to void * for display - but no conversion exists for volatile pointers. As such C++ falls back to the implicit cast to bool . Cast to void* explicitly if you want to print the address: std::cout << (void*)&clock; 回答2: There's an operator<< for const void* , but there's no operator<< for volatile void*

Hide user input on password prompt [duplicate]

↘锁芯ラ 提交于 2019-11-27 04:25:19
Possible Duplicate: Read a password from std::cin I don't work normally with the console, so my question is maybe very easy to answer or impossible to do . Is it possible to "decouple" cin and cout , so that what I type into the console doesn't appear directly in it again? I need this for letting the user typing a password and neither me nor the user normally wants his password appearing in plaintext on the screen. I tried using std::cin.tie on a stringstream , but everything I type is still mirrored in the console. From How to Hide Text : Windows #include <iostream> #include <string> #include

operator << must take exactly one argument

纵饮孤独 提交于 2019-11-27 04:14:48
问题 a.h #include "logic.h" ... class A { friend ostream& operator<<(ostream&, A&); ... }; logic.cpp #include "a.h" ... ostream& logic::operator<<(ostream& os, A& a) { ... } ... When i compile, it says: std::ostream& logic::operator<<(std::ostream&, A&)' must take exactly one argument. What is the problem? 回答1: The problem is that you define it inside the class, which a) means the second argument is implicit ( this ) and b) it will not do what you want it do, namely extend std::ostream . You have

How can I open a file for reading & writing, creating it if it does not exist, without truncating it?

夙愿已清 提交于 2019-11-27 03:54:27
问题 What is the proper set of I/O flags for a std::fstream , where I want to be able to read from and write to the file, without truncating the file if it exists, but creating it if it does not? I've tried std::ios::binary | std::ios::in | std::ios::out std::ios::binary | std::ios::in | std::ios::out | std::ios::ate but neither of these create the file if it does not already exist. I don't want std::ios::app , because I also need to be able to seek around the file at will, with both the get and

CRT的来历

◇◆丶佛笑我妖孽 提交于 2019-11-27 02:24:57
之前由于要研究一下VC编译选项对于最终编译出来的模块尺寸的影响,所以就顺便研究了一下Windows的CRT库的相关知识,收集如下: (转载自:http://www.cnblogs.com/chio/archive/2007/11/26/972152.html) [关于CRT]   CRT原先是指Microsoft开发的C Runtime Library,用于操作系统的开发及运行。后来在此基础上开发了C++ Runtime Library,所以现在CRT是指Microsoft开发的C/C++ Runtime Library。在VC的CRT/SRC目录下,可以看到CRT的源码,不仅有C的,也有C++的。   CRT原先的目的就是支持操作系统的运行。因为Windows操作系统除汇编部分外,都是用C/C++编写的,所以内核及许多关键服务都在CRT上运行(它们都采用dll技术动态链接)。此外,用 VC编写的C/C++程序也用到它们(可以动态链接,也可以静态链接,前者运行时需要系统中已安装CRT的dll,后者不需要)。可以说,CRT就是 Microsoft编写Windows时使用的低层类库。然后,它又被当作C++标准库的一个实现包含在了VC系列中;我们在VC中使用的C++标准库, 其实就是CRT的一个真子集(少了C++标准所不包含的代码,特别是大量的低层C代码)。  

How can I read from memory just like from a file using iostream?

放肆的年华 提交于 2019-11-27 02:23:40
问题 I have simple text file loaded into memory. I want to read from memory just like I would read from a disc like here: ifstream file; string line; file.open("C:\\file.txt"); if(file.is_open()) { while(file.good()) { getline(file,line); } } file.close(); But I have file in memory. I have an address in memory and a size of this file. What I must do to have the same fluency as with dealing with file in the code above? 回答1: You can do something like the following.. std::istringstream str; str.rdbuf

C++ read from istream until newline (but not whitespace)

百般思念 提交于 2019-11-27 02:09:12
问题 I have a std::istream which refers to matrix data, something like: 0.0 1.0 2.0 3.0 4.0 5.0 Now, in order to assess the number of columns I would like to have some code like: std::vector<double> vec; double x; while( (...something...) && (istream >> x) ) { vec.push_back(x); } //Here vec should contain 0.0, 1.0 and 2.0 where the ...something... part evaluates to false after I read 2.0 and istream at the point should be at 3.0 so that the next istream >> x; should set x equal to 3.0. How would

Do I have to use #include <string> beside <iostream>?

做~自己de王妃 提交于 2019-11-27 01:44:19
I started learning C++ and I read a book which writes that I must use the <string> header file because the string type is not built directly into the compiler. If I use the <iostream> I can use the string type. Do I have to include the <string> header when I want to use the string type if I included the <iostream> header? Why? Is there some difference? Yes, you have to include what you use. It's not mandated that standard headers include one another (with a few exceptions IIRC). It might work now, but might fail on a different compiler. In your case, apparently <iostream> includes <string> ,

How do the stream manipulators work?

只谈情不闲聊 提交于 2019-11-27 01:21:34
It is well known that the user can define stream manipulators like this: ostream& tab(ostream & output) { return output<< '\t'; } And this can be used in main() like this: cout<<'a'<<tab<<'b'<<'c'<<endl; Please explain me how does this all work? If operator<< assumes as a second parameter a pointer to the function that takes and returns ostream & , then please explain my why it is necessary? What would be wrong if the function does not take and return ostream & but it was void instead of ostream & ? Also it is interesting why “dec”, “hex” manipulators take effect until I don’t change between

What's the difference between while(cin) and while(cin >> num)

时光总嘲笑我的痴心妄想 提交于 2019-11-27 01:01:57
问题 What the difference between the following two loops and When each one will stopped ? #include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int x,y; while(cin >> x){ // code } while(cin){ cin >> y; //code } return 0; } 回答1: Let's look at these independently: while(cin >> x) { // code } This loop, intuitively, means "keep reading values from cin into x , and as long as a value can be read, continue looping." As soon as a value is read that isn't an int , or