iostream

Input line by line from an input file and tokenize using strtok() and the output into an output file

别来无恙 提交于 2019-11-27 12:28:24
问题 What I am trying to do is to input a file LINE BY LINE and tokenize and output into an output file.What I have been able to do is input the first line in the file but my problem is that i am unable to input the next line to tokenize so that it could be saved as a second line in the output file,this is what i could do so far fro inputing the first line in the file. #include <iostream> #include<string> //string library #include<fstream> //I/O stream input and output library using namespace std;

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

一笑奈何 提交于 2019-11-27 11:32:39
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 variables involved. Another example is when you want to output a byte, unformatted, to an ostream using its put function. Any ideas? Note : I'm still not really convinced. So if you know the definitive answer, you can still post it indeed. Possibly I've misunderstood

What is the <iosfwd> header?

限于喜欢 提交于 2019-11-27 11:25:31
What's the <iosfwd> header used for? Why is it necessary? Any example? It's so you can declare, in your own headers, methods that rely on the declarations of iostream types without having to #include the iostream headers themselves, which are large, complex and slow to compile against. Here's a simple example: // foo.h #include <iosfwd> void sucker(std::iostream& is); // foo.cc #include <iostream> void sucker(std::iostream& is) { is >> somevar; } Adam Rosenfield As @Marcelo Cantos mentioned, it's so you can include the declaration of iostream classes and functions without including the full

Are there binary memory streams in C++

对着背影说爱祢 提交于 2019-11-27 11:18:25
I usually use stringstream to write into in-memory string. Is there a way to write to a char buffer in binary mode? Consider the following code: stringstream s; s << 1 << 2 << 3; const char* ch = s.str().c_str(); The memory at ch will look like this: 0x313233 - the ASCII codes of the characters 1, 2 and 3. I'm looking for a way to write the binary values themselves. That is, I want 0x010203 in the memory. The problem is that I want to be able to write a function void f(ostream& os) { os << 1 << 2 << 3; } And decide outside what kind of stream to use. Something like this: mycharstream c; c << 1

How can I detect if a type can be streamed to an std::ostream?

依然范特西╮ 提交于 2019-11-27 10:20:09
问题 I'm trying to write a type trait to detect if a type has overloaded operator<<() suitable to use to an output stream. I'm missing something because I'm always getting true for a simple empty class with no operators at all. Here the code: template<typename S, typename T> class is_streamable { template<typename SS, typename TT> static auto test(SS&& s, TT&& t) -> decltype(std::forward<SS>(s) << std::forward<TT>(t)); struct dummy_t {}; static dummy_t test(...); using return_type = decltype(test

What's the real reason to not use the EOF bit as our stream extraction condition?

泄露秘密 提交于 2019-11-27 08:56:46
Inspired by my previous question A common mistake for new C++ programmers is to read from a file with something along the lines of: std::ifstream file("foo.txt"); std::string line; while (!file.eof()) { file >> line; // Do something with line } They will often report that the last line of the file was read twice. The common explanation for this problem (one that I have given before) goes something like: The extraction will only set the EOF bit on the stream if you attempt to extract the end-of-file, not if your extraction just stops at the end-of-file. file.eof() will only tell you if the

uint8_t iostream behavior

狂风中的少年 提交于 2019-11-27 07:45:13
问题 Abstract: I was expecting the code: cout << uint8_t(0); to print "0", but it doesn't print anything. Long version: When I try to stream uint8_t objects to cout, I get strange characters with gcc. Is this expected behavior? Could it be that uint8_t is an alias for some char-based type? See compiler/system notes in the code example. // compile and run with: // g++ test-uint8.cpp -std=c++11 && ./a.out // -std=c++0x (for older gcc versions) /** * prints out the following with compiler: * gcc (GCC

Unexpected results with std::ofstream binary write

自闭症网瘾萝莉.ら 提交于 2019-11-27 07:43:08
问题 I'm new to C++ std::stream and I'm making some tests. I have this simple code: int i = 10; char c = 'c'; float f = 30.40f; std::ofstream out("test.txt", std::ios::binary | std::ios::out); if(out.is_open()) { out<<i<<c<<f; out.close(); } As the stream is opened as std::ios::binary I expect in the test.txt file to have the binary representation of i , c and f , but instead I have 10c30.4 . Can you please tell me what I'm doing wrong? 回答1: std::ios::binary promises to not do any line-end

Reading and writing a std::vector into a file correctly

拟墨画扇 提交于 2019-11-27 07:27:15
That is the point. How to write and read binary files with std::vector inside them? I was thinking something like: //============ WRITING A VECTOR INTO A FILE ================ const int DIM = 6; int array[DIM] = {1,2,3,4,5,6}; std::vector<int> myVector(array, array + DIM); ofstream FILE(Path, ios::out | ofstream::binary); FILE.write(reinterpret_cast<const char *>(&myVector), sizeof(vector) * 6); //=========================================================== But I don't know how to read this vector. Because I thought that the following was correctly but it isn't: ifstream FILE(Path, ios::in |

Is it possible to define enumalpha?

泄露秘密 提交于 2019-11-27 07:24:50
问题 I would like to be able to write: cout << enumalpha << Monday; and get printed on console: Monday P.S. Monday is an enum type. 回答1: Okay, let's go all preprocessor then :) Intended way of use: DEFINE_ENUM(DayOfWeek, (Monday)(Tuesday)(Wednesday) (Thursday)(Friday)(Saturday)(Sunday)) int main(int argc, char* argv[]) { DayOfWeek_t i = DayOfWeek::Monday; std::cout << i << std::endl; // prints Monday std::cin >> i >> std::endl; // reads the content of a string and // deduce the corresponding enum