iostream

Reading files larger than 4GB using c++ stl

笑着哭i 提交于 2019-11-26 16:10:39
问题 A few weeks back I was using std::ifstream to read in some files and it was failing immediately on open because the file was larger than 4GB. At the time I couldnt find a decent answer as to why it was limited to 32 bit files sizes, so I wrote my own using native OS API. So, my question then: Is there a way to handle files greater than 4GB in size using std::ifstream/std::ostream (IE: standard c++) EDIT: Using the STL implementation from the VC 9 compiler (Visual Studio 2008). EDIT2: Surely

How can I print 0x0a instead of 0xa using cout?

倖福魔咒の 提交于 2019-11-26 16:03:38
问题 How can I print 0x0a, instead of 0xa using cout? #include <iostream> using std::cout; using std::endl; using std::hex; int main() { cout << hex << showbase << 10 << endl; } 回答1: This works for me in GCC: #include <iostream> #include <iomanip> using namespace std; int main() { cout << "0x" << setfill('0') << setw(2) << hex << 10 << endl; } If you are getting sick and tired of iostream's formatting quirkiness, give Boost.Format a try. It allows good-old-fashioned, printf-style format specifiers

Why do C++ streams use char instead of unsigned char?

被刻印的时光 ゝ 提交于 2019-11-26 15:37:14
问题 I've always wondered why the C++ Standard library has instantiated basic_[io]stream and all its variants using the char type instead of the unsigned char type. char means (depending on whether it is signed or not) you can have overflow and underflow for operations like get(), which will lead to implementation-defined value of the variables involved. Another example is when you want to output a byte, unformatted, to an ostream using its put function. Any ideas? Note : I'm still not really

Are there binary memory streams in C++

亡梦爱人 提交于 2019-11-26 15:30:58
问题 I usually use stringstream to write into in-memory string. Is there a way to write to a char buffer in binary mode? Consider the following code: stringstream s; s << 1 << 2 << 3; const char* ch = s.str().c_str(); The memory at ch will look like this: 0x313233 - the ASCII codes of the characters 1, 2 and 3. I'm looking for a way to write the binary values themselves. That is, I want 0x010203 in the memory. The problem is that I want to be able to write a function void f(ostream& os) { os << 1

How to read line by line or a whole text file at once?

拈花ヽ惹草 提交于 2019-11-26 15:06:40
I'm in a tutorial which introduces files (how to read and write from\to file) First of all, this is not a homework, this is just general help I'm seeking. I know how to read one word at a time, but I don't know how to read one line at a time or how to read the whole text file. What if my file contains 1000 words? It is not practical to read each word. My text file named (Read) contains the following: I love to play games I love reading I have 2 books This is what I have accomplished so far: #include <iostream> #include <fstream> using namespace std; int main (){ ifstream inFile; inFile.open(

What&#39;s the real reason to not use the EOF bit as our stream extraction condition?

我怕爱的太早我们不能终老 提交于 2019-11-26 14:23:55
问题 Inspired by my previous question A common mistake for new C++ programmers is to read from a file with something along the lines of: std::ifstream file("foo.txt"); std::string line; while (!file.eof()) { file >> line; // Do something with line } They will often report that the last line of the file was read twice. The common explanation for this problem (one that I have given before) goes something like: The extraction will only set the EOF bit on the stream if you attempt to extract the end

How to read until EOF from cin in C++

时光怂恿深爱的人放手 提交于 2019-11-26 14:15:20
I am coding a program that reads data directly from user input and was wondering how could I (without loops) read all data until EOF from standard input. I was considering using cin.get( input, '\0' ) but '\0' is not really the EOF character, that just reads until EOF or '\0' , whichever comes first. Or is using loops the only way to do it? If so, what is the best way? The only way you can read a variable amount of data from stdin is using loops. I've always found that the std::getline() function works very well: std::string line; while (std::getline(std::cin, line)) { std::cout << line << std

Prevent scientific notation in ostream when using << with double

柔情痞子 提交于 2019-11-26 13:46:20
I need to prevent my double to print in scientific notation in my file, when I do this outfile << X; davecoulter To set formatting of floating variables you can use a combination of setprecision(n) , showpoint and fixed . In order to use parameterized stream manipulators like setprecision(n) you will have to include the iomanip library: #include <iomanip> setprecision(n) : will constrain the floating-output to n places, and once you set it, it is set until you explicitly unset it for the remainder of the stream output. fixed : will enforce that all floating-point numbers are output the same

How to get IOStream to perform better?

我怕爱的太早我们不能终老 提交于 2019-11-26 12:41:35
Most C++ users that learned C prefer to use the printf / scanf family of functions even when they're coding in C++. Although I admit that I find the interface way better (especially POSIX-like format and localization), it seems that an overwhelming concern is performance. Taking at look at this question: How can I speed up line by line reading of a file It seems that the best answer is to use fscanf and that the C++ ifstream is consistently 2-3 times slower. I thought it would be great if we could compile a repository of "tips" to improve IOStreams performance, what works, what does not.

Using flush() before close()

点点圈 提交于 2019-11-26 12:40:08
问题 As per the java docs, invoking close() on any java.io Streams automatically invokes flush(). But I have seen in lot of examples, even in production codes, developers have explicitly used flush() just before close(). In what conditions we need to use flush() just before close()? 回答1: Developer get into a habit of calling flush() after writing something which must be sent. IMHO Using flush() then close() is common when there has just been a write e.g. // write a message out.write(buffer, 0,