iostream

C++: Reading from text file until EOF repeats last line

The following C++ code uses a ifstream object to read integers from a text file (which has one number per line) until it hits EOF. Why does it read the integer on the last line twice? How to fix this? Code: #include <iostream> #include <fstream> using namespace std; int main() { ifstream iFile("input.txt"); // input.txt has intege...

Which I/O library do you use in your C++ code?

In new C++ code, I tend to use the C++ iostream library instead of the C stdio library. I've noticed some programmers seem to stick to stdio, insisting that it's more portable. Is this really the case? What do you use? ...

C++ having cin read a return character

Hey everyone, I was wondering how to use cin so that if the user does not enter in any value and just pushes ENTER that cin will recognize this as valid input. Thanks in advance, Tomek ...

C++: "std::endl" vs "\n"

Many C++ books contain example code like this... std::cout << "Test line" << std::endl; ...so I've always done that too. But I've seen a lot of code from working developers like this instead: std::cout << "Test line\n"; Is there a technical reason to prefer one over the other, or is it just a matter of coding style? ...

Is there a way to check if an istream was opened in binary mode?

I'm using an istream which could be stringstream, ifstream or a user-defined stream type and I need to know if, in the case of an ifstream, it was not opened in binary mode (so I can throw an exception). I have tried the following method: if ((_is.flags() & ios::binary) == 0) throw exception(...) but no exception is ever thrown. T...

iostream linker error

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 <iost...

Fastest way to create large file in c++ ?

Create a flat text file in c++ say 50 - 100 MB say the content like 'Added first line' should be inserted in to the file for 40 lakhs time ...

Kill a blocked Boost::Thread

I am writing an application which blocks on input from two istreams. Reading from either istream is a synchronous (blocking) call, so, I decided to create two Boost::threads to do the reading. Either one of these threads can get to the "end" (based on some input received), and once the "end" is reached, both input streams stop receivin...

std::wcout to console window in XCode

In an XCode project, if I use std::cout to write to the console the output is fine. However, if I use std::wcout I get no output. I know that this is a thorny issue in C++, and I've been googling around to try and find a specific solution in the XCode case. A couple of things I found that it was suggested should work were: std::c...

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

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 variabl...

Reading files larger than 4GB using c++ stl.

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 ...

Cross-platform (linux/Win32) nonblocking C++ IO on stdin/stdout/stderr

I'm trying to find the best solution for nonblocking IO via stdin/stdout with the following characteristics: As long as there is enough data, read in n-sized chunks. If there's not enough data, read in a partial chunk. If there is no data available, block until there is some (even though it may be smaller than n). The goal is to allo...

What is the best way to make the output of one stream the input to another

I'm wondering if there is a better/inbuilt way, other than using a byte buffer and looping, to read from one stream and write it to another (in .NET). Generally this is done to apply a transform to a stream and move it on. In this instance, what I am loading a file, putting it through a deflate stream and writing it out to a file (Error...

How can I discover/control the level of internal buffering in a C++ fstream?

Say I do this (a contrived example): #include <iostream> #include <fstream> using namespace std; int main(int argc, char* argv[]) { ifstream ifs(argv[1]); char ch; while(ifs.read(&ch, 1)) { cout << ch; } } I assume(hope) that the iostream library does some internal buffering here and doesn't turn this into g...

std::ifstream::open() not working

Hi all, I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini file so that the game designers can tweak the game parameters without requiring help from me in addition to a re-compile. This is what I'm doing currently: std::ifstream stream; stream.open("rules.ini"); if (!stream.is_open()) { ...

Obtain a std::ostream either from std::cout or std::ofstream(file)

Hi, how do I bind a std::ostream to either std::cout or to an std::ofstream object, depending on a certain program condition? Although this invalid for many reasons, I would like to achieve something that is semantically equivalent to the following: std::ostream out = condition ? &std::cout : std::ofstream(filename); I've seen some e...

Why are System.out/err implemented as Byte Streams in Java?

I was having a look at this tutorial at Sun on command line I/O. It stated that: You might expect the Standard Streams to be character streams, but, for historical reasons, they are byte streams. System.out and System.err are defined as PrintStream objects. Although it is technically a byte stream, PrintStream utilizes an...

Can I use CreateFile, but force the handle into a std::ofstream?

Is there any way to take advantage of the file creation flags in the Win32 API such as FILE_FLAG_DELETE_ON_CLOSE or FILE_FLAG_WRITE_THROUGH as described here http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx , but then force that handle into a std::ofstream? The interface to ofstream is obviously platform independent; I'd lik...

How to properly overload the << operator for an ostream?

I am writing a small matrix library in C++ for matrix operations. However my compiler complaints, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian 4.3.2-1.1) 4.3.2 ) however I have the same problem on a Ubuntu system with the same g++. Here i...

Can you explain the concept of streams?

I understand that a stream is a representation of a sequence of bytes. Each stream provides means for reading and writing bytes to its given backing store. But what is the point of the stream? Why isn't the backing store itself what we interact with? For whatever reason this concept just isn't clicking for me. I've read a bunch of...