iostream

C++ how to add/subtract tellp(), tellg() return

倖福魔咒の 提交于 2019-12-12 17:20:52
问题 Say I wanted to get the difference (in int) between two tellp() outputs. The tellp() output could be huge if a big file is written so it is not safe to say store it inside a long long. Is there a safe way to perform a operation like this: ofstream fout; fout.open("test.txt",ios::out | ios::app); int start = fout.tellp(); fout<<"blah blah "<<100<<","<<3.14; int end = fout.tellp(); int difference = end-start; Here, I know that the difference between end and start can definitely fit in an int.

Cin has no operand >>

 ̄綄美尐妖づ 提交于 2019-12-12 16:08:02
问题 I don't understand why this isn't working. For some reason I'm getting the error: error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) I'm doing this in Visual Studio2010 C++ Express if that helps. Not sure why its handing me this error I've done other programs using cin ... My Code: #include <iostream> #include <iomanip> #include <fstream> using namespace std; int main(int argc, char* argv){ string file; if

std::stringstream and std::ios::binary

久未见 提交于 2019-12-12 10:47:45
问题 I want to write to a std::stringstream without any transformation of, say line endings. I have the following code: void decrypt(std::istream& input, std::ostream& output) { while (input.good()) { char c = input.get() c ^= mask; output.put(c); if (output.bad()) { throw std::runtime_error("Output to stream failed."); } } } The following code works like a charm: std::ifstream input("foo.enc", std::ios::binary); std::ofstream output("foo.txt", std::ios::binary); decrypt(input, output); If I use a

Netbeans: cannot find include file <iostream>, also, unable to resolve identifier std, cout

别等时光非礼了梦想. 提交于 2019-12-12 09:38:37
问题 I'm receiving the above errors on all my C++ projects on Netbeans. I also receive cannot find errors for anything else I try to include. Here's what it says when I hold control and hover over it: http://imgur.com/bBF2xuB. One of the projects actually builds and runs just fine, but all the rest fail either with "build failed, exit value 2" or the run fails with "run failed, exit value 127". I have absolutely no idea how to fix this and everything I've looked up to try to solve this hasn't

iostream linker error

怎甘沉沦 提交于 2019-12-12 08:23:07
问题 I have some old C code that I would like to combine with some C++ code. The C code used to have has the following includes: #include <windows.h> #include <stdio.h> #include <string.h> #include "mysql.h" Now I'm trying to make it use C++ with iostream like this: #include <windows.h> #include <stdio.h> #include <string> #include <iostream> #include "mysql.h" But I keep getting the following linker errors when I compile: [Linker error] undefined reference to `std::string::size() const' [Linker

What could cause a stream to enter the “bad” state?

谁说胖子不能爱 提交于 2019-12-12 07:27:10
问题 In C++, each stream has a bad bit: This flag is set by operations performed on the stream when an error occurs while read or writing data, generally causing the loss of integrity of the stream. Source What would cause a stream to "lose integrity" and enter the bad state? This is not the same as the fail state, which most often occurs when an input stream attempts to store a value into a variable that cannot accept said value (such as attempting to store a string into an integer variable).

Getting better error messages for iostreams

筅森魡賤 提交于 2019-12-12 05:58:38
问题 I implemented a small program that can extract (and via fuse mount) a certain archive format. I use boost::filesystem::ifstream , but on error (e.g. the file a user wants to extract does not exist) I get very nondescript error messages. I wonder is there a way to get better error messages for IO related problems in C++? On a related note I wonder whether I should have used C's FILE* or in the case of the fuse filesystem just plain file descriptors? Because strerror(errno) is way better than

im counting the number of characters in a file but i want to count the number of words that are less than 5 and 6 or greater

醉酒当歌 提交于 2019-12-12 04:29:43
问题 i want to do this: reads the words in the file one at a time. (Use a string to do this) Counts three things: how many single-character words are in the file, how many short (2 to 5 characters) words are in the file, and how many long (6 or more characters) words are in the file. HELP HERE im not sure on how about reading file into a string. i know i have to something like this but i dont understand the rest. HELP HERE ifstream infile; //char mystring[6]; //char mystring[20]; int main() {

std::ofstream is adding carriage return (CR; \r) after \n automatically

我怕爱的太早我们不能终老 提交于 2019-12-12 03:59:12
问题 I am trying to write PPM file on disk. PPM is a simple image format that consists of ASCII image header and byte array of pixels: P6\n width height\n 255\n [width*height*3 bytes total] This is my PPM class (simplified): class PPMImage { protected: friend std::istream& operator >>(std::istream &inputStream, PPMImage &other); friend std::ostream& operator <<(std::ostream&, const PPMImage&); size_t width; size_t height; // eg. "P6" std::string magicNumber; // Normally 255 uint16_t maxBrightness;

Using ICU to implement my own codecvt facet

孤街醉人 提交于 2019-12-12 03:06:38
问题 I want to implement a codecvt facet using ICU to convert from any character encoding (that ICU supports) to UTF-8 internally. I'm aware that codecvt_byname exists and that it can be used to do part of what I want as shown in this example. The problems with that example are that it (1) uses wide character streams (I want to use "regular", byte-oriented streams) and (2) requires 2 streams to perform the conversion. Instead, I want a single stream like: locale loc( locale(), new icu_codecvt(