iostream

Difference between ios::app and ios::ate [duplicate]

被刻印的时光 ゝ 提交于 2019-12-18 11:45:58
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C++ Filehandling: Difference between ios:app and ios:ate? What is the difference between these two file opening modes ? ios:ate sets the get/put pointer position to the end of the file => reading/writing will begin from end, but how is it different from ios::app, which again opens a file in append mode...but when I have created a ofstream and opened it in ios:app mode, the put stream pointer still points to the

Why use endl when I can use a newline character? [duplicate]

懵懂的女人 提交于 2019-12-18 09:59:45
问题 This question already has answers here : C++: “std::endl” vs “\n” (13 answers) Closed 2 years ago . Is there a reason to use endl with cout when I can just use \n ? My C++ book says to use endl, but I don't see why. Is \n not supported as widely as endl , or am I missing something? 回答1: endl appends '\n' to the stream and calls flush() on the stream. So cout << x << endl; is equivalent to cout << x << '\n'; cout.flush(); A stream may use an internal buffer which gets actually streamed when

Input Validation to make sure only number c++

孤街浪徒 提交于 2019-12-18 09:21:13
问题 Ok, I'm trying to get good at using pointers so I'm trying to write a input validation for the user input to make sure that anything that isn't a number is handled correctly. When I use isdigit() isn't working for me. I still get an exception when I enter a alphabet. Any suggestions? Thanks. Check this out: #include<iostream> #include<algorithm> #include<string> #include<cctype> using namespace std; void EnterNumbers(int * , int); int main() { int input = 0; int *myArray; cout << "Please

Why two EOF needed as input? [duplicate]

Deadly 提交于 2019-12-18 07:06:29
问题 This question already has an answer here : Canonical vs. non-canonical terminal input (1 answer) Closed 4 years ago . When I run the code below, I use three inputs (in Ubuntu terminal): abc(Ctrl+D)(Ctrl+D) abc(Ctrl+D)(Enter)(Ctrl+D) abc(Enter)(Ctrl+D) The code reacts well in all cases. My question is: why in 1) and 2) I need two EOF? #include <iostream> int main() { int character; while((character=std::cin.get())!=EOF){} std::cout << std::endl << character << std::endl; } 回答1: That's how the

No matching constructor for initalization of 'ostream_iterator<int>'

自古美人都是妖i 提交于 2019-12-18 06:25:04
问题 for the code, why error, osteam_iterator is a template class ,why no matching constructor for initalization of 'ostream_iterator', please give a help , thank you. define ostream_iterator template > class _LIBCPP_VISIBLE ostream_iterator int main(int argc, const char * argv[]) { vector<int> sentence1; sentence1.reserve(5);// 设置每次分配内存的大小 sentence1.push_back(1); sentence1.push_back(2); sentence1.push_back(3); sentence1.push_back(4); sentence1.push_back(5); int c = 5; copy(sentence1.begin(),

No matching constructor for initalization of 'ostream_iterator<int>'

南笙酒味 提交于 2019-12-18 06:24:07
问题 for the code, why error, osteam_iterator is a template class ,why no matching constructor for initalization of 'ostream_iterator', please give a help , thank you. define ostream_iterator template > class _LIBCPP_VISIBLE ostream_iterator int main(int argc, const char * argv[]) { vector<int> sentence1; sentence1.reserve(5);// 设置每次分配内存的大小 sentence1.push_back(1); sentence1.push_back(2); sentence1.push_back(3); sentence1.push_back(4); sentence1.push_back(5); int c = 5; copy(sentence1.begin(),

C++ iostream vs. C stdio performance/overhead

霸气de小男生 提交于 2019-12-18 05:17:05
问题 I'm trying to comprehend how to improve the performance of this C++ code to bring it on par with the C code it is based on. The C code looks like this: #include <stdlib.h> #include <stdio.h> #include <string.h> typedef struct point { double x, y; } point_t; int read_point(FILE *fp, point_t *p) { char buf[1024]; if (fgets(buf, 1024, fp)) { char *s = strtok(buf, " "); if (s) p->x = atof(s); else return 0; s = strtok(buf, " "); if (s) p->y = atof(s); else return 0; } else return 0; return 1; }

std::ostream_iterator prevent last item from using the delimiter [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-18 03:32:21
问题 This question already has answers here : Printing lists with commas C++ (27 answers) Closed 3 years ago . Is there a way to use a std::ostream_iterator (or similar) such that the delimiter isn't placed for the last element? #include <iterator> #include <vector> #include <algorithm> #include <string> using namespace std; int main(int argc, char *argv[]) { std::vector<int> ints = {10,20,30,40,50,60,70,80,90}; std::copy(ints.begin(),ints.end(),std::ostream_iterator<int>(std::cout, ",")); } Will

NaN ASCII I/O with Visual C++

点点圈 提交于 2019-12-18 03:16:10
问题 I want to read and write NaN values from/into text files using iostream and Visual C++. When writing a NaN value, i get 1.#QNAN . But, reading it back outputs 1.0 . float nan = std::numeric_limits<float>::quiet_NaN (); std::ofstream os("output.txt"); os << nan ; os.close(); The output is 1.#QNAN . std::ifstream is("output.txt"); is >> nan ; is.close(); nan equals 1.0 . Solution Finally, as suggested by awoodland, I've come up with this solution. I chose "nan" as a string representation of a

ifstream: check if opened successfully

别来无恙 提交于 2019-12-17 23:23:59
问题 A colleague just told me that this code: std::ifstream stream(filename.c_str()); if (!stream) { throw std::runtime_error(".."); } would be wrong. He said ifstream evaluates to 0 if opening is successful. My code works, but I wanted to find the documentation but didn't see where it says how to check if opening was successful. Can you point me to it? 回答1: operator! is overloaded for std::ifstream , so you can do this. In my opinion, though, this is a horrible abuse of operator overloading (by