iostream

How to read a file line by line to a string type variable?

旧街凉风 提交于 2019-12-07 05:46:26
问题 I'm trying to read a file line by line to a string type variable using the following code: #include <iostream> #include <fstream> ifstream file(file_name); if (!file) { cout << "unable to open file"; exit(1); } string line; while (!file.eof()) { file.getline(line,256); cout<<line; } file.close(); it won't compile when I try to use String class, only when I use char file[256] instead. how can I get line by line into a string class? 回答1: Use std::getline : std::string s; while (std::getline

Extending C++ ostream

一个人想着一个人 提交于 2019-12-07 05:31:46
问题 I'm trying to learn more about the workings of the C++ I/O stream library by extending std::streambuf . As a learning experiment, my goal is to simply create a custom stream which directs all output to std::cerr . It seems simple enough: #include <iostream> using namespace std; class my_ostreambuf : public std::streambuf { public: protected: std::streamsize xsputn(const char * s, std::streamsize n) { std::cerr << "Redirecting to cerr: " << s << std::endl; return n; } }; int main() { my

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

大城市里の小女人 提交于 2019-12-07 04:42:29
问题 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 handling removed for simplicity): byte[] buffer = new byte[10000000]; using (FileStream fsin = new FileStream(filename, FileMode.Open)) { using (FileStream

Change or check the openmode of a std::ofstream

梦想与她 提交于 2019-12-07 04:34:03
问题 In some code that does a lot of file i/o using std::ofstream , I'm caching the stream for efficiency. However, sometimes I need to change the openmode of the file (e.g. append vs truncate). Here is some similar mock code: class Logger { public: void write(const std::string& str, std::ios_base::openmode mode) { if (!myStream.is_open) myStream.open(path.c_str(), mode); /* Want: if (myStream.mode != mode) { myStream.close(); myStream.open(path.c_str(), mode); } */ myStream << str; } private: std

Inserters and Extractors reading/writing binary data vs text

泄露秘密 提交于 2019-12-07 04:01:14
问题 I've been trying to read up on iostreams and understand them better. Occasionally I find it stressed that inserters ( << ) and extractors ( >> ) are meant to be used in textual serialization. It's a few places, but this article is a good example: http://spec.winprog.org/streams/ Outside of the <iostream> universe, there are cases where the << and >> are used in a stream-like way yet do not obey any textual convention. For instance, they write binary encoded data when used by Qt's QDataStream

Getting a byte value using stringstream

六眼飞鱼酱① 提交于 2019-12-07 03:32:11
问题 I've got this (incorrect) sample code for getting a value out of stringstream and storing it in a byte-sized variable (it needs to be in a single byte var, not an int): #include <iostream> #include <sstream> using namespace std; int main(int argc, char** argv) { stringstream ss( "1" ); unsigned char c; ss >> c; cout << (int) c << endl; } The output when I run this is 49, which is not what I would like to see. Obviously, this is being treated as a char and not simple numeric value. What is the

PrintWriter vs PrintStream vs OutputStreamWriter timecosts

我的梦境 提交于 2019-12-07 03:06:27
问题 As you know we have several tools in java for writing data into streams. In this sample code I have compared them by runtime. Can somebody explain it exactly? Thanks. Here is the code: import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.PrintWriter; public class IOtests { public static void main(String[] args) throws Exception { char[] chars = new char[100]; byte[] bytes = new byte[100]; for (int i = 0; i < 100; i++) { chars[i] =

Check if all values were successfully read from std::istream

一笑奈何 提交于 2019-12-07 02:27:19
问题 Let's say I have a file that has 100 text If I try reading 2 numbers using ifstream, it would fail because text is not a number. Using fscanf I'll know it failed by checking its return code: if (2 != fscanf(f, "%d %d", &a, &b)) printf("failed"); But when using iostream instead of stdio, how do I know it failed? 回答1: Its actually as (if not more) simple: ifstream ifs(filename); int a, b; if (!(ifs >> a >> b)) cerr << "failed"; Get used to that format, by the way. as it comes in very handy

STL, iostream, new, delete in C/C++ for CUDA

我们两清 提交于 2019-12-07 01:54:09
问题 Can I use STL,iostream,new, delete in C/C++ for CUDA? 回答1: If you have a Fermi class GPU (so compute capability >=2.0), and are using CUDA 4.0 or later, then both new and delete are avialable for use in device code. STL containers and algorithms and iostream are not supported. If you want to use "STL like" operations with CUDA, you might be interested in the Thrust template library. It allows host code to transparently interact with the GPU using container types and implements a number of

How do I format a floating point value so that it never uses exponent notation nor has trailing zeros?

风流意气都作罢 提交于 2019-12-07 00:33:17
问题 According to ios_base manipulators, I basically have the choice between defaultfloat and fixed when formatting floating point numbers without exponent notation (with decimal numbers). However, I want to choose the maximum precision which would produce a lot trailing zeros for fixed for many numbers (e.g. 1. ) but avoid ever using the exponent notation. If set to defaultfloat , it will look right most of the time, unless the value is really really small, yet not 0. . In that case, the default