iostream

How to read content of .EXE file in Java

倾然丶 夕夏残阳落幕 提交于 2019-12-22 17:38:28
问题 What are the possible options and the most appropiate for reading an executable file in Java. I want produce the hexadecimal representation of an .exe file. Im thinking of reading the file in binary and then doing the conversion. But how can i read the .exe? 回答1: 1) read the file in as bytes. use BufferedInputStream( new FileInputStream( new File("bin.exe") ) ) 2) convert each byte to hex format. static final String HEXES = "0123456789ABCDEF"; public static String getHex( byte [] raw ) { if (

How to clear/reset/open an input stream so it can be used in 2 different methods in Java?

送分小仙女□ 提交于 2019-12-22 13:07:15
问题 Here's the code: package testpack; import java.io.*; public class InputStreamReadDemo { private void readByte() throws IOException { System.out.print("Enter the byte of data that you want to be read: "); int a = System.in.read(); System.out.println("The first byte of data that you inputted corresponds to the binary value "+Integer.toBinaryString(a)+" and to the integer value "+a+"."); // tried writing System.in.close(); and System.in.reset(); } private void readLine() throws IOException {

迭代器

依然范特西╮ 提交于 2019-12-22 12:28:26
1.插入迭代器     |back_inserter:创建使用push_back实现末端插入的迭代器。   |front_inserter:使用push_front实现前端插入。(vector或其他没有push_front运算的容器上不能使用)   |inserter:使用insert实现指定位置插入操作,除了所关联的容器外,inserter还带有第二个实参:指向插入起始位置的迭代器。 2.iostream迭代器(iostream不是容器,但是标准库同样提供了在iostream对象上使用的迭代器)   |istream_iterator:用于读取输入流。   |ostream_iterator:用于写输出流。 3.反向迭代器(支持--和++操作的迭代器都可以定义反向迭代器)   |reserve_iterator:vec.rbegin()返回容器vec的最后一个元素的迭代器;vec.rend()返回容器vec的第一个元素的前一个元素的迭代器。 4.const迭代器   |const_iterator:避免使用迭代器来修改容器中的元素。 来源: https://www.cnblogs.com/qiushuixiaozhanshi/p/5630617.html

Efficient way to implement a bounded string formatting operator for use with <<?

给你一囗甜甜゛ 提交于 2019-12-22 10:23:10
问题 I'm looking for something where: string pearString ("pear"); string bananaString ("banana"); cout << ???? 5 ??? pearString ??????? << "[end]" << endl; cout << ???? 5 ??? bananaString ??????? << "[end]" << endl; Will (for some sequence of code characters ???) output: pear[end] banan[end] But I'd like this to work without having to do a substring operation that would copy the string. Unless I'm missing something, there is no formatting specifier for this. (The setw specifier pads shorter

seekg() failing mysteriously

╄→гoц情女王★ 提交于 2019-12-22 00:34:28
问题 I have a 2884765579 bytes file. This is double checked with this function, that returns that number: size_t GetSize() { const size_t current_position = mFile.tellg(); mFile.seekg(0, std::ios::end); const size_t ret = mFile.tellg(); mFile.seekg(current_position); return ret; } I then do: mFile.seekg(pos, std::ios::beg); // pos = 2883426827, which is < than the file size, 2884765579 This sets the failbit. errno is not changed. What steps can I take to troubleshoot this? I am absolutely sure

Are C++ strings and streams buffer overflow safe?

跟風遠走 提交于 2019-12-21 09:08:02
问题 If I use std::cin, std::cout and std::string, is there any possibility that someone will exploit the buffer overflow? I ask this because I still see a lot of people that still use null-terminated strings instead of standard containers in C++. 回答1: It depends. Of course, when you use C-style code/API's, there is no difference . But using STL or C++ idioms doesn't guarantee that you're safe. C++ gives you the choice, always. Contrast these two near-identical twins: int n; std::cin >> n; std:

Print function for class c++

房东的猫 提交于 2019-12-21 08:16:08
问题 I want to write a print function for a class AutoData that has information about cars in it. With this print function, I would ideally like to print out a vector that contains many different class objects. I have already written get functions for each element of the objects, but I am still a bit unsure of how to go about using those to write a function to print out the data in the following format: mpg:cylinders:displacement:horsepower:weight:acceleration:modelYear:origin:carName For example:

Why does ostream prints `1` for a string defined as `volatile char[]`? [duplicate]

泄露秘密 提交于 2019-12-21 07:04:11
问题 This question already has answers here : Why does std::cout convert volatile pointers to bool? (4 answers) Closed 5 years ago . Consider this (artificial) example: #include <cstdio> #include <iostream> int main() { volatile char test[] = "abc"; std::printf("%s\n", test); std::cout << test << "\n"; } Compiling it with GCC and running gives the following output: $ g++ test.cc $ ./a.out abc 1 As you can see printf prints the string correctly while cout prints 1 . Why does writing to cout

C++ duplicate stdout to file by redirecting cout

风格不统一 提交于 2019-12-21 05:54:05
问题 Good day. I have to use some external functions that produce a lot of debugging information to stdout (via std::cout ). I want to duplicate this information to some log file by redirecting cout to boost tee_device . I use the following example code: typedef boost::iostreams::tee_device<ostream, ofstream> TeeDevice; typedef boost::iostreams::stream<TeeDevice> TeeStream; int main(int argc, char** argv) { remove("file.log"); ofstream logFile; logFile.open("file.log"); TeeDevice outputDevice(cout

When does `ifstream::readsome` set `eofbit`?

痞子三分冷 提交于 2019-12-21 04:08:34
问题 This code loops forever: #include <iostream> #include <fstream> #include <sstream> int main(int argc, char *argv[]) { std::ifstream f(argv[1]); std::ostringstream ostr; while(f && !f.eof()) { char b[5000]; std::size_t read = f.readsome(b, sizeof b); std::cerr << "Read: " << read << " bytes" << std::endl; ostr.write(b, read); } } It's because readsome is never setting eofbit . cplusplus.com says: Errors are signaled by modifying the internal state flags: eofbit The get pointer is at the end of