iostream

istream from file_descriptor_source (boost::iostreams) or file

I need to do something like this for my program's input: stream input; if (decompressed) input.open(filepath); else { file_descriptor=_popen("decompressor "+filepath,"r"); input.open(file_descriptor); } input.read(...) ... I can see 1 solution... to use _popen in both cases and just copy the file to stdout if it's already ...

How to use pass ifstream input in a function and output? C++

For example: ifstream input; input.open("file.txt"); translateStream(input, cout); input.close(); How to write function translateStream? void translateStream(XXXX input, YYYY output)? What are the types for input and output? Thanks ...

Can getline() be used to get a char array from a fstream

Hi, I want to add a new (fstream) function in a program that already uses char arrays to process strings. The problem is that the below code yields strings, and the only way i can think of getting this to work would be to have an intermediary function that would copy the strings, char by char, into a new char array, pass these on to the...

How to create a "default" stream insertion operator in C++?

I have a class similar to boost::any, in that it is a templated container class. I'd like to have a method to write the contained value to a string. However, if the contained type doesn't provide a stream insertion operator, I'd like my method to return some default tring rather than failing to compile. Below is as close as I've come, an...

C++ cin.fail() question

When running the following code and enter a number, it works fine. But when entering a letter, the program enters an infinite loop, displaying "Enter a number (0 to exit): cin failed." My intent was to handle the cin fail case and prompt the user again. int number; do{ cout << "Enter a number (0 to exit): "; cin >> number; ...

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

Hi, 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 har...

Texture loading at joGL

hi I've been trying to load a bmp picture to use it as a texture at my program I've used a IOstream Class to extend DataInputStream to read the pixels at the photo with this code "based on a texture loader code for c++ " : //class Data members public static int BMPtextures[]; public static int BMPtexCount = 30; public static int curr...

When is it good to use c++ iostreams over ReadFile, WriteFile, fprintf, etc ...?

I find that it is tremendously easier to use streams in c++ instead of windows functions like ReadFile, WriteFile, etc or even fprintf. When is it not good to use streams? When is it good to use streams? Is it safe to use streams? How come a lot of programmers don't use streams? This is just something I've always wondered about and mayb...

Why does endl get used as a synonym for "\n" even though it incurs significant performance penalties?

This program: #include <iostream> #include <cstdlib> #include <string> int main(int argc, const char *argv[]) { using ::std::cerr; using ::std::cout; using ::std::endl; if (argc < 2 || argc > 3) { cerr << "Usage: " << argv[0] << " [<count>] <message>\n"; return 1; } unsigned long count = 10000; if (arg...

C++: assign cin to an ifstream variable?

You know the common stdio idiom that stdin is specified by a filename of "-", e.g. if ((strcmp(fname, "-")) fp = fopen(fname); else fp = stdin; What's the best way to do this with an ifstream instance? I've received a bit of code that has an ifstream as part of a class and I'd like to add code to do the equivalent, something ...

iostream use of << to construct string

How can << be used to construct a string ala int iCount; char szB[128]; sprintf (szB,"%03i", iCount); ...

C++ - Passing std::ostream to a function

Hi I thought of a small debug inline function in C++: void inline debug( int debug_level, ostream& out ) { if ( debug_level <= verbosity ) { out.flush(); } else { ostream tmp; tmp << out; } } This is an example of how I wanted to use it: _debug( 7, cout << "Something something" << someint << e...

Overload handling of std::endl ?

I want to define a class MyStream so that: MyStream myStream; myStream << 1 << 2 << 3 << std::endl << 5 << 6 << std::endl << 7 << 8 << std::endl; gives output [blah]123 [blah]56 [blah]78 Basically, I want a "[blah]" inserted at the front, then inserted after every non terminating std::endl ? The difficulty here is NOT the logic ma...

declaring generic istream in c++

I need to write a program that reads in either from ifstream or cin, depending on parameters passed into the program at runtime. I was planning on doing the following: istream in; if(argv[1] == "cin") { in = cin; } else { ifStream inFile; inFile.open(argv[1].c_str()); in = inFile; } However, istream in...

Get all folders from TFS using TFS SDK

Hi All, I am creating a TFS tool that will get "changeset information" from the TFS server. Now, I want to provide a "TFS Browser" so that the user can browse what "branch/folder" he wants to fetch information from. I am using a TreeView control and the GetItems function to get the items' path from TFS: private void treeView1_BeforeE...

What does the "c" mean in cout, cin, cerr and clog?

What does the "c" mean in the cout, cin, cerr and clog names? I would say char but I haven't found anything to confirm it. ...

Restore the state of std::cout after manipulating it.

Hello Everyone, Suppose I have a code like this: void printHex(std::ostream& x){ x<<std::hex<<123; } .. int main(){ std::cout<<100; // prints 100 base 10 printHex(std::cout); //prints 123 in hex std::cout<<73; //problem! prints 73 in hex.. } My question is if there is any way to 'restore' the state of cout to its orig...

While loop with try catch fails at bad cin input

I can't seem to figure out why this falls into a loop after getting non-int input. I've tried cin.flush(), which doesn't seem to exist, cin.clear(), which seems like it should work, even cin.sync() after reading someone else post about it working, but didn't seem to make much sense. Also tried cin.bad(). Thank you very much for any help...

When should I concern myself with std::iostream::sentry?

Online references have rather brief and vague descriptions on the purpose of std::iostream::sentry. When should I concern myself with this little critter? If it's only intended to be used internally, why make it public? ...

Does anyone actually use stream extraction operators?

I've written tons of operator<<(std::ostream &, const T &) functions -- they're incredibly useful. I've never written an operator>>(std::istream &, T &) function in real code or even used the extraction operators for built-in types (OK, maybe for std::string). Are these appropriate only for short example programs and textbooks? Is ope...