stringstream

infile.open refuses to read the variable in the file

岁酱吖の 提交于 2019-12-11 19:18:39
问题 So, I have this loop: int counter1 = 0; ifstream incard; string card; string cardname; stringstream out; while (counter1 < 4) { counter1 = counter1 + 1; out << counter1; out << ".card"; card = out.str(); cout << card; system("PAUSE"); incard.open(card.c_str()); incard >> cardname; cout << cardname << endl; incard.close(); out.str(""); } 1.card contains text "Angel" 2.card contains text "Devil" 3.card contains text "Firaxis" 4.card contains text "Robert" This is the output I get: 1.cardPress

Comma Separate Number Without stringstream

时光总嘲笑我的痴心妄想 提交于 2019-12-11 07:33:15
问题 So typically if I wanted to insert locale appropriate separators in some number, foo , I'd do something like this: ostringstream out; out.imbue(locale("en-US")); out << foo; Then I could just use out.str() as the separated string: http://coliru.stacked-crooked.com/a/054e927de25b5ad0 Unfortunately I've been asked not to use stringstreams in my current project. Is there any other way I can accomplish this? Ideally a locale dependent way? 回答1: So this answer is the C++ distillation of Jerry

snprintf vs std::stringstream

泄露秘密 提交于 2019-12-11 03:48:04
问题 I am working on removing SECURITY CODING violation in my product. My code has lot of sprintf , coverity tool is suggesting me to use snprintf , But C++ also has std::stringstream . Will it be a good idea to use std::stringstream in place of snprintf 回答1: std::stringstream is typesafe, use operator <<, use internal buffer, part of C++, not so fast as sprintf . sprintf is not typesafe, not use c++ operators, use external buffer, use elipsis, which can works only with POD types, inherited from C

sgetn Doesn't Null Terminate String

你。 提交于 2019-12-10 22:39:23
问题 sgetn Takes a char* for it's first argument and writes characters to it. It does not write a trailing '\0' to the char* . This behavior seems to be inconsistent with every other time that I can find a char* written to. However, it is consistent across Clang, gcc, and Visual Studio, so I can't believe it's a bug that all the compilers have. Is there a reason that the standard doesn't require the trailing '\0' to the char* ? [Live Example] 回答1: Because it can be used to read arbitrary data, not

How to block on reading a c++ stringstream to wait for data

你。 提交于 2019-12-10 17:35:53
问题 So, I've been trying to figure out, how to wait for data from a C++ stringstream (for instance), without being constantly checking if data is there, which is quite CPU consuming. I'm perfectly able to read, for instance, from a serial device, and lock the process while no data arrives, but unfortunately I haven't been able to figure how to do that with C++ streams. I'm sure I'm missing something, since cin does exactly that, i.e., waits for the return key to step out from istream reading, but

C++ stringstream >> int returns zero

天大地大妈咪最大 提交于 2019-12-10 16:35:45
问题 I'm writing a function to return the reverse of a number i.e it converts int(1234) to int(4321) . This is what I have currently: #include <iostream> #include <cstdlib> #include <string> #include <sstream> using namespace std; int reverse(int num) { stringstream ss (stringstream::in | stringstream::out); string initial; int reversed; // read the number in to a string stream ss << num; initial = ss.str(); // flush the stringstream ss.str(""); for(unsigned int i(0); i <= initial.size(); i++) {

Inserting endline into a stringstream

心不动则不痛 提交于 2019-12-10 14:14:54
问题 We know that when inserting \n in a file stream, the appropriate end-of-line sequence for the system will be written to the file (e.g. \r\n for Windows). Does inserting an endline in a std::stringstream result in the system-appropriate end-of-line sequence being written to the string? For example: #include <sstream> int main() { std::ostringstream oss; oss << std::endl; std::string endlineSequence = oss.str(); bool isWindows = enlineSequence == "\r\n"; bool isOldMac = endlineSequence == "\r";

atof and stringstream produce different results

天涯浪子 提交于 2019-12-10 05:45:16
问题 I have been looking into a problem whereby I am converting a float to a human readable format, and back. Namely a string. I have ran into issues using stringstream and found that atof produces "better" results. Notice, I do not print out the data in this case, I used the debugger to retrieve the values: const char *val = "73.31"; std::stringstream ss; ss << val << '\0'; float floatVal = 0.0f; ss >> floatVal; //VALUE IS 73.3100052 floatVal = atof(val); //VALUE IS 73.3099976 There is probably a

Ways std::stringstream can set fail/bad bit?

给你一囗甜甜゛ 提交于 2019-12-10 02:26:41
问题 A common piece of code I use for simple string splitting looks like this: inline std::vector<std::string> split(const std::string &s, char delim) { std::vector<std::string> elems; std::stringstream ss(s); std::string item; while(std::getline(ss, item, delim)) { elems.push_back(item); } return elems; } Someone mentioned that this will silently "swallow" errors occurring in std::getline . And of course I agree that's the case. But it occurred to me, what could possibly go wrong here in practice

Incorrect string value: '\xB2\xE5\xC8\xEB\xD6\xD0...' for column 'file_path' at row 1

北城以北 提交于 2019-12-10 00:00:21
1、问题描述: 在C++中封装的MySQL操作在插入中文时失败。返回失败信息如下: Incorrect string value: '\xB2\xE5\xC8\xEB\xD6\xD0...' for column 'file_path' at row 1 2、问题分析: 刚开始时的时候我在VS中打印出要插入的中文字符是乱码,以为是编译器和数据库的编码格式的问题,在网上找了解决方案都是修改服务器的编码格式,或者修改MySQL Server 8.0 Data中my.ini中的编码格式,但是都没作用。最后一步步调试跟踪后发现: (1)乱码问题: 乱码问题是我在将 stringstream 转换为string时使用了mysql.h中的: unsigned long STDCALL mysql_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length); 导致的问题,实际上在转换时使用 stringstream 中的str()函数即可解决。 (2)插入中文失败问题: 向数据库插入中文字符失败是发生在最后调用mysql.h中的mysql_query()发生了错误。即实际上在插入中文的时候最后是使用了以下函数: int STDCALL mysql_query(MYSQL *mysql,