iostream

Resetting the End of file state of a ifstream object in C++

大兔子大兔子 提交于 2019-11-28 02:34:56
问题 I was wondering if there was a way to reset the eof state in C++? 回答1: For a file, you can just seek to any position. For example, to rewind to the beginning: std::ifstream infile("hello.txt"); while (infile.read(...)) { /*...*/ } // etc etc infile.clear(); // clear fail and eof bits infile.seekg(0, std::ios::beg); // back to the start! If you already read past the end, you have to reset the error flags with clear() as @Jerry Coffin suggests. 回答2: Presumably you mean on an iostream. In this

Why does the rvalue overload of `operator<<` for `basic_ostream` return an lvalue reference?

爱⌒轻易说出口 提交于 2019-11-28 02:27:04
问题 §27.7.3.9 defines the following overload for operator<< : template <class charT, class traits, class T> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&& os, const T& x); Effects: os << x Returns: os ( §27.7.2.6 defines the rvalue overload for operator>> .) Basically, it just forwards to an lvalue overload. I consider this overload to be pretty dangerous (the istream one even more so than the ostream one, actually), consider the following: #include <sstream> #include

structure

白昼怎懂夜的黑 提交于 2019-11-28 02:12:51
不管是什么东东,理论是一部分,我个人觉得更重要的一环还是在应用上,所以在这里叙述性的东西比较少,我挑了几个关于结构体重点的部分来学习结构体,对其进行一一举例,我想通过例子,理解起来将会更容易,更准确,更深刻。 1, 有关结构体类型 结构体定义的一般形式 struct 结构体名 { 类型数据 成员名 1 ; 类型数据 成员名 2 ; ….... 成员名 ...; 类型数据 成员名 n; };// 最后一个分号标志着类型定义的结束 说明:( 1 )结构体内的成员名不能重名,但可以与结构体之外的同名 ( 2 )结构体类型占用的存储空间的字节数等于所有成员占用存储空间字节数的总和 结构体所占用的字节数 #include " iostream " using namespace std; struct date { int year ; int month; int day; }; struct list { int num; char name[ 10 ]; char sex; int age; char add[ 20 ]; float score; }; int main() { cout << sizeof ( struct date) << endl << sizeof ( struct list) << endl; } 2 ,结构体变量的引用 引用的一般形式 结构体变量名 .

C++ iostream Corruption using stringstream

微笑、不失礼 提交于 2019-11-28 02:05:45
I'm trying to write a really simple thread-safe logger. Ideally, I wanted it to work just like std::cout , wherein you could just overload the << operator and have everything magically show up in the log. I'm on a Windows machine, so here's the approach I tried: // Threadsafe logger class Logger { public: Logger() { InitializeCriticalSection(&s); } ~Logger() { DeleteCriticalSection(&s); } void Log(std::ostream const& os) { EnterCriticalSection(&s); //std::cout << static_cast<std::stringstream const&>(os).str(); std::cout << os.rdbuf(); LeaveCriticalSection(&s); } private: CRITICAL_SECTION s; }

Removing a comma from a c++ output

时光毁灭记忆、已成空白 提交于 2019-11-28 02:03:54
I wrote this program that sorts numbers in vectors from greatest to least, it works great but the only thing that is bugging me is trying to remove the comma from the last number. Heres my code #include <iostream> #include <vector> #include <cstdlib> #include <algorithm> using namespace std; int main() { vector<int> vi1, vi2, vi3; srand(987); for (int i = 0; i < 10; ++i) vi1.push_back(rand() % 10); sort(vi1.begin(), vi1.end()); for (int i = 0; i < 10; ++i) vi2.push_back(rand() % 10); sort(vi2.begin(), vi2.end()) while(!vi1.empty() && !vi2.empty()) { if(vi1.back()>=vi2.back()) { vi3.push_back

how to use isatty() on cout, or can I assume that cout == file descriptor 1?

两盒软妹~` 提交于 2019-11-28 01:57:02
Well, the subject says it all, basically. I have a command-line utility that may be used interactively or in scripts, using pipes or i/o redirection. I am using cin and cout for i/o, and I want to write an extra EOL at the end if the output is console, so that user prompt will start from the next line. Within scripts this would be harmful. Can I assume cin == 0, cout == 1 ? I understand that there is no clean way to get the file descriptor of a stream. Or is it? It is possible to use rdbuf() to change the destination of std::cin and std::cout inside your program. If you don't do that, it is

Why don't iostream objects overload operator bool?

笑着哭i 提交于 2019-11-28 01:03:37
In this answer I talk about using a std::ifstream object's conversion to bool to test whether the stream is still in a good state. I looked in the Josuttis book for more information (p. 600 if you're interested), and it turns out that the iostream objects actually overload operator void* . It returns a null pointer when the stream is bad (which can be implicitly converted to false ), and a non-null pointer otherwise (implicitly converted to true ). Why don't they just overload operator bool ? Potatoswatter This is an instance of the "safe bool" problem. Here is a good article: http://www

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

烈酒焚心 提交于 2019-11-28 00:42:39
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] . 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). The strerror function from <cstring> might be useful. This isn't necessarily standard or portable, but it works okay for me using GCC on an Ubuntu box: #include <iostream> using std::cout; #include <fstream> using

Can I stop std::cout flushing on “\\n”?

懵懂的女人 提交于 2019-11-28 00:29:02
According to to this post std::cout will automatically flush on \n when it is attached to an interactive device (e.g. a terminal window). Otherwise (e.g. when being piped to a file) it will act fully buffered and will only flush on .flush() or std::endl . Is there a way to override this behaviour in Microsoft Visual C++ so that I can select whether I want fully buffered or line buffered mode? Contrary to anon's (Apr 28 '09) answer, this behavior has nothing to do with the operating system or "console software." C++'s <iostream> streams are designed to be interoperable with C's <stdio.h>

Characters extracted by istream >> double

放肆的年华 提交于 2019-11-28 00:12:39
Sample code at Coliru : #include <iostream> #include <sstream> #include <string> int main() { double d; std::string s; std::istringstream iss("234cdefipxngh"); iss >> d; iss.clear(); iss >> s; std::cout << d << ", '" << s << "'\n"; } I'm reading off N3337 here (presumably that is the same as C++11). In [istream.formatted.arithmetic] we have (paraphrased): operator>>(double& val); As in the case of the inserters, these extractors depend on the locale’s num_get<> (22.4.2.1) object to perform parsing the input stream data. These extractors behave as formatted input functions (as described in 27.7