iostream

Infix vs prefix syntax: name lookup differences

天大地大妈咪最大 提交于 2019-12-01 15:39:48
Operators in C++ are usually considered to be an alternative syntax for functions/methods, especially in the context of overloading. If so, the two expressions below should be synonymous: std::cout << 42; operator<<(std::cout, 42); In practise, the second statement leads to the following error: call of overloaded ‘operator<<(std::ostream&, int)’ is ambiguous As usual, such error message is accompanied with a list of possible candidates, these are: operator<<(basic_ostream<_CharT, _Traits>& __out, char __c) operator<<(basic_ostream<char, _Traits>& __out, char __c) operator<<(basic_ostream<char,

Why does endl get used as a synonym for “\n” even though it incurs significant performance penalties?

末鹿安然 提交于 2019-12-01 15:06:20
问题 This program: #include <iostream> #include <cstdlib> #include <string> int main(int argc, const char *argv[]) { using ::std::cerr; using ::std::cout; using ::std::endl; if (argc < 2 || argc > 3) { cerr << "Usage: " << argv[0] << " [<count>] <message>\n"; return 1; } unsigned long count = 10000; if (argc > 2) { char *endptr = 0; count = ::std::strtoul(argv[1], &endptr, 10); if ((argv[1][0] == '\0') || (*endptr != '\0')) { cerr << "Usage: " << argv[0] << " [<count>] <message>\n"; return 1; } }

How to have a file stream as a class member

老子叫甜甜 提交于 2019-12-01 12:11:52
问题 I have the following parser class that works in Visual C++ class Parser { private: const char* filename; std::ifstream filestream; std::vector<std::string> tokens; unsigned int linect; public: Parser(const char* filename); bool readline(); std::string getstrtoken(unsigned int i) const { return tokens[i]; } int getinttoken(unsigned int i) const { return atoi(tokens[i].c_str()); } }; Parser::Parser(const char* filename) : filename(filename), linect(0) { filestream = ifstream(filename); // OK in

C++ user input restriction with proper retry without “goto”

拥有回忆 提交于 2019-12-01 11:44:12
I have the following code: qstn: cout << "Input customer's lastname: "; getline(cin, lname); if (lname.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ") != string::npos) { cout << "You can only input alpha here!\n"; cin.clear(); goto qstn; } else if (lname.empty()) { cout << "Please enter your firstname!\n"; cin.clear(); goto qstn; } int lnamel = lname.length(); int strl = str.length(); int is = 0; for (int i = 1; i < strl;) { i++; is++; if (lname[i] == lname[is] && lname[i] == ' ' || lname[0] == ' ') { cin.clear(); cout << "Please input your lastname properly!\n";

Is the default mode of ofstream implementation defined?

北城以北 提交于 2019-12-01 11:37:28
问题 Given the following code: std::ofstream stream("somefile"); if (!stream) { return 1; } When invoking .write(....) and using stdc++ and libc++ the stream is in binary mode ( std::ios::binary ). However when using MSVC (2015/2017RC1) it seems to be in text mode or something weird, because the the resulting file is larger than what is actually written. But if i explicitly set the mode std::ios::binary MSVC behaves similarly to the std::ofstream implementations of other standard libraries

C++ Detect input redirection [duplicate]

瘦欲@ 提交于 2019-12-01 11:29:18
Possible Duplicate: Detect if stdin is a terminal or pipe in C/C++/Qt? Consider we got a small program which takes some standard C input. I would like to know if the user is using input redirection, for example like this: ./programm < in.txt Is there a way to detect this way of input redirecting in the program? There's no portable way to do that, since C++ says nothing about where cin comes from. On a Posix system, you can test whether or not cin comes from a terminal or is redirected using isatty , something like this: #include <unistd.h> if (isatty(STDIN_FILENO)) { // not redirected } else {

trying to write std:out and file at the same time

 ̄綄美尐妖づ 提交于 2019-12-01 08:45:23
I am trying to write to file and stdout at the same time within c++ by overloading ofstream test.h #pragma once #include <iostream> using std::ofstream; class OutputAndConsole:public ofstream { public: std::string fileName; OutputAndConsole(const std::string& fileName):ofstream(fileName),fileName(fileName){ }; template <typename T> OutputAndConsole& operator<<(T var); }; template <typename T> OutputAndConsole& OutputAndConsole::operator<<(T var) { std::cout << var; ofstream::operator << (var); return (*this); }; test.cpp OutputAndConsole file("output.txt"); file << "test" ; The output in the

C++ Detect input redirection [duplicate]

筅森魡賤 提交于 2019-12-01 08:40:25
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Detect if stdin is a terminal or pipe in C/C++/Qt? Consider we got a small program which takes some standard C input. I would like to know if the user is using input redirection, for example like this: ./programm < in.txt Is there a way to detect this way of input redirecting in the program? 回答1: There's no portable way to do that, since C++ says nothing about where cin comes from. On a Posix system, you can

C++ buffered stream IO

旧城冷巷雨未停 提交于 2019-12-01 08:34:34
I understand that by default all stream IO supported by C++ is buffered. This means that data to be output is put into a buffer till it is full and then sent to the output device, similarly for input, the data is read once the buffer is empty...all this is done so that number of expensive system calls could be minimized. But how to verify this behavior in action. I mean consider the following code int main() { cout << "Hello world\n"; return 0 } Where does buffering come into picture here ? I know there is buffering happening, but how to explain it? The output is seen instantly on the screen,

C++ user input restriction with proper retry without “goto”

你说的曾经没有我的故事 提交于 2019-12-01 07:36:34
问题 I have the following code: qstn: cout << "Input customer's lastname: "; getline(cin, lname); if (lname.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ") != string::npos) { cout << "You can only input alpha here!\n"; cin.clear(); goto qstn; } else if (lname.empty()) { cout << "Please enter your firstname!\n"; cin.clear(); goto qstn; } int lnamel = lname.length(); int strl = str.length(); int is = 0; for (int i = 1; i < strl;) { i++; is++; if (lname[i] == lname[is] &&