iostream

stdio vs iostream [closed]

↘锁芯ラ 提交于 2019-11-30 13:59:42
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . When I search on the internet for the difference between these two libraries, everyone says that <iostream> is the standard I/O

Using boost in WDK build environment for applications?

为君一笑 提交于 2019-11-30 13:26:45
I am using the Windows Driver Kit (WinDDK 6001.18001) to build my userspace application rather than Visual Studio 2005. I am taking this approach because we also have to build driver components, so I'd prefer to have a single build environment to build everything. Microsoft itself uses this approach for several products. This was working fine until I started using Boost 1.38.0. I'm not using C++ in the kernel mode components, just the userspace applications. In C++ code, it's natural to use the boost libraries. Unfortunately, the WDK doesn't agree. The first error I noticed is that "#include

How to read formatted data in C++?

余生颓废 提交于 2019-11-30 13:12:55
问题 I have formatted data like the following: Words 5 AnotherWord 4 SomeWord 6 It's in a text file and I'm using ifstream to read it, but how do I separate the number and the word? The word will only consist of alphabets and there will be certain spaces or tabs between the word and the number, not sure of how many. 回答1: Assuming there will not be any whitespace within the "word" (then it will not be actually 1 word), here is a sample of how to read upto end of the file: std::ifstream file("file

Are standard output streams in C++ thread-safe (cout, cerr, clog)?

你。 提交于 2019-11-30 12:42:23
I know that there is no concept of threads in current C++ , but this article is saying : A typesafe, threadsafe, portable logging mechanism ..... The fprintf() function is threadsafe, so even if this log is used from different threads, the output lines won't be scrambled. What about cout , cerr and clog ? I think this question is applicable to all kind of stream types in C++ also, like fstream and stringstream . The article makes a claim about the POSIX standard for the fprintf API. It says nothing about C++ streams. And this is quite correct, as there are no such guarantees on those stream.

Why is it that wcout << “”; is OK but wcout << string(); is not?

拥有回忆 提交于 2019-11-30 11:19:01
#include <iostream> #include <string> using namespace std; int main() { wcout << L"Hello"; // OK. wcout << wstring(L"Hello"); // OK. wcout << "Hello"; // OK. Why? wcout << string("Hello"); // Error. Why? } Why does std::wcout accept a narrow string literal as its argument but doesn't accept a narrow string object? This is dictated by § 27.7.3.6.4 of the C++11 Standard, where the following two overloaded operators (among others) are specified: template<class charT, class traits> basic_ostream<charT,traits>& operator<<( basic_ostream<charT,traits>& out, const charT* s ); template<class charT,

When to use printf/scanf vs cout/cin?

Deadly 提交于 2019-11-30 09:20:07
I'm testing some snippets I found off the web using g++ from MinGW. This is the C++ compiler...why then does it correctly compile C....why do people intertwine C and C++. The concrete question is: Is it O.K. to use both C and C++ and compile under g++. If the answer is yes, this makes my life easy as I do not have to modify the code. Oddly enough...to get some C++ to work, particularly when passing a string to an ifstream constructor it requires a C type string... My guess would be that because C++ depends upon C constructs at times is is O.K to write the two languages together. However as a

Why does string extraction from a stream set the eof bit?

泄露秘密 提交于 2019-11-30 09:04:20
Let's say we have a stream containing simply: hello Note that there's no extra \n at the end like there often is in a text file. Now, the following simple code shows that the eof bit is set on the stream after extracting a single std::string . int main(int argc, const char* argv[]) { std::stringstream ss("hello"); std::string result; ss >> result; std::cout << ss.eof() << std::endl; // Outputs 1 return 0; } However, I can't see why this would happen according to the standard (I'm reading C++11 - ISO/IEC 14882:2011(E) ). operator>>(basic_stream<...>&, basic_string<...>&) is defined as behaving

How to instantiate an fstream if you declare it as a member of a class?

这一生的挚爱 提交于 2019-11-30 08:32:21
问题 What constructor can you use to instantiate an fstream if you declare it as a member of a class? #include <fstream> class Foo { Foo(); // not allowed std::fstream myFile("\\temp\\foo.txt", fstream::in | fstream::out | fstream::trunc); // allowed std::fstream myFile; } // constructor Foo::Foo() { // what form of myFile("\\temp\\foo.txt", fstream::in | fstream::out | fstream::trunc) can I use here? myFile = ??? } 回答1: In the new version of C++ (C++11), then the above code you have is perfectly

How is std::iostream buffered?

淺唱寂寞╮ 提交于 2019-11-30 08:22:23
General use case I am trying to implement a basic shell. Description I need to read user input until some delimiters are pressed so a corresponding action can be performed. Those delimiter could be a single 'a', a single 'b' or a single 'c'. An input example would look like this (where > is the shell prompt): > 111-222-333-444a Ok, '111-222-333-444' entered Why do I want inline delimiter instead of 'new-line' delimiter? Because I would like to listen to keyboard event such as 'up-arrow' to erase the current command and print the last command (implementing a history feature). Because I would

How can I make Unicode iostream i/o work in both Windows and Unix-land?

时光怂恿深爱的人放手 提交于 2019-11-30 07:33:12
问题 Note: This is a question-with-answer in order to document a technique that others might find useful, and in order to perhaps become aware of others’ even better solutions. Do feel free to add critique or questions as comments. Also do feel free to add additional answers. :) Problem #1: Console support for Unicode via streams is severely limited at the Windows API level. The only relevant codepage available for ordinary desktop applications is 65001, UTF-8. And then interactive input fails at