iostream

Seeking in a file that is more than 4GB in C++? [duplicate]

醉酒当歌 提交于 2019-12-10 17:38:06
问题 This question already has answers here : Reading files larger than 4GB using c++ stl (5 answers) Closed 4 years ago . I am using the MS Visual Studio 2012 compiler and I am building in x64 release mode. Using ifstream I can read files larger than 4GB. The problem is, I can't seek to a position in the middle of a 10GB file. When I use seekg like this is.seekg (5368709120, is.beg); then is.tellg(); returns -1 which means the seek failed. I am sure that the file exists and the position

Construct ofstream from stdio file

陌路散爱 提交于 2019-12-10 17:24:54
问题 Is it possible to create an ofstream object from a given stdio file (old-style C file handle)? Preferably without closing and re-opening the file. The purpose for this is I have a stdio file and a library I'm using takes a stream object. 回答1: If using boost is an option, and the library takes std::basic_ostream (as opposed to an std::ofstream ) you could use a File Descriptor Sink to wrap your file descriptor, construct a stream from of it, and pass it to your target library. 来源: https:/

Read a very long console input in C++

☆樱花仙子☆ 提交于 2019-12-10 16:48:47
问题 I'm trying to read a list of numbers (space delimited) from the console using std::cin. When the input line is longer than 1023 characters, the first "cin >> list[i]" in the following small working example never returns: using namespace std; int main() { vector<int> list(200,0); for(int i=0;i<200;i++){ cin >> list[i]; cout << '-'; cin.clear(); if(list[i]==0) break; } return 0; } This code fails for the following input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

How does the scheme below garantees there'll be just one definition for the objects cin, cout, …?

萝らか妹 提交于 2019-12-10 15:54:21
问题 Here you'll find the following statements under Which header ? : Finally, <iostream> provides the eight standard global objects (cin, cout, etc). To do this correctly, this header also provides the contents of the <istream> and <ostream> headers, but nothing else. The contents of this header look like #include <ostream> #include <istream> namespace std { extern istream cin; extern ostream cout; .... // this is explained below static ios_base::Init __foo; // not its real name } Now, the

C++ Decorate basic_iostream classes

半城伤御伤魂 提交于 2019-12-10 14:18:50
问题 I want to do something like the following code shows: class foo { private: std::fstream* m_stream; public: foo(std::fstream* stream) : m_stream(stream) { } foo& write(char const* s, std::streamsize count) { if (/*condition*/) { m_stream->write(s, count); } else { // ... } return *this; } foo& read(char* s, std::streamsize count) { if (/*condition*/) { m_stream->read(s, count); } else { // ... } return *this; } }; I would need to add the same behavior to all similar methods (e.g. put ). This

Why doesn't std::istream assume ownership over its streambuf?

微笑、不失礼 提交于 2019-12-10 13:19:27
问题 I am writing some sort of virtual file system library for video-games in the likes of CRI Middleware's ROFS (see Wikipedia). My intention with the library is to provide natural means of accessing the resources of the games I develop, which store some data embedded in the executable, some on the media and some on the local user's hard drive (preferences, save game files, etc). Access to such resources should be as simple as making a call like std::auto_ptr<std::istream> defaultConfigIStream(

Why does Clang std::ostream write a double that std::istream can't read?

微笑、不失礼 提交于 2019-12-10 12:33:33
问题 I am using an application that uses std::stringstream to read a matrix of space separated double s from a text file. The application uses code a little like: std::ifstream file {"data.dat"}; const auto header = read_header(file); const auto num_columns = header.size(); std::string line; while (std::getline(file, line)) { std::istringstream ss {line}; double val; std::size_t tokens {0}; while (ss >> val) { // do stuff ++tokens; } if (tokens < num_columns) throw std::runtime_error {"Bad data

Correct implementation of std::streambuf::overflow

会有一股神秘感。 提交于 2019-12-10 11:24:42
问题 I am creating a special std::streambuf and std::ostream implementation. For that I need to implement the std::streambuf::overflow function. The function is protected, only called by std::streambuf::sputc and std::streambuf::xsputn and only when there is no more room in the buffer (that is pptr() == epptr() ). The default behaviour is to return eof , so it is obviously not correct to call the function in place of sputc . Nevertheless both GNU libc implementation of std::stringbuf and boost

How to read float with scientific notation from file C++?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 09:47:27
问题 I have a file with this format: -0.0064785667 0.73900002 0.028505694 4.7858757e-39 315 218 -0.0051828534 0.73900002 0.028505694 4.6936954e-39 316 218 -0.0038818798 0.73799998 0.028467119 5.1546736e-39 317 218 -0.0025879198 0.73799998 0.028467119 5.6160217e-39 318 218 -0.0012939599 0.73799998 0.028467119 6.4461411e-39 319 218 I read it with this code: using namespace std; ifstream inputFile; //Opens data file for reading. inputFile.open(filePath.c_str()); //Creates vector, initially with 0

c++ console screen size

僤鯓⒐⒋嵵緔 提交于 2019-12-10 09:30:13
问题 So I'm learning some stuff in College on C++, and the teacher and I got into a discussion on how to actually center text to the output screen. So my suggestion was to use setw but get the length of the string and the size of the console screen, do the algorithm and BAM we have truly centered text. He says the screen size is 80 but the screen can be resized, which doesn't work no matter what if the output is centered the the user starts resizing. Just a minor question I have, how to get the