[英]C++ templated ofstream characters printing in decimal
I am making a generic logger class which I prints anything passed into a file. 我正在制作一个通用的记录器类,它可以打印传递到文件中的所有内容。
Logger::Instance()->write('c');
Logger::Instance()->write("this");
Logger::Instance()->write(5);
However I run into an issue where I have: 但是我遇到了一个我有的问题:
char c = 't';
Logger::Instance()->write(c);
Which prints decimal instead of the ascii: 哪个显示十进制而不是ASCII:
116
Strings work fine but characters are the only thing having issues. 字符串可以正常工作,但是字符是唯一有问题的东西。
My code: 我的代码:
class Logger {
public:
static Logger* Instance();
bool open(string filename);
template<typename T>
void write(T data) {
file << data;
}
bool close();
If I make a char specific printing function: 如果我执行char特定的打印功能:
void write(char data) {
file << data;
It will print the character. 它将打印字符。 Not sure what is going on here.
不知道这是怎么回事。
Edit 1: 编辑1:
Just a summary.. So why does this print the numerical: 只是一个摘要。所以为什么要打印数字:
void write(T data) {
file << data;
}
And this print the correct ascii: 这将打印正确的ascii:
void writeChar(char data) {
file << data;
}
??? ???
Edit 2: 编辑2:
Here is my code: 这是我的代码:
logger.h logger.cpp logger.h logger.cpp
Use this class and just go: 使用此类,然后去:
char c = 't';
Logger::Instance()->write(t);
Logger::Instance()->writeChar(t);
Edit 3: 编辑3:
Alright.. here is the last source file. 好..这是最后一个源文件。
[keyboardhook.cpp][3]
[keyboardhook.h][4]
[main.cpp][5]
[3]: http://pastebin.com/VEav4kfn
[4]: http://pastebin.com/yPwF17yg
[5]: http://pastebin.com/JwHz2NBe
Take a look here: 在这里看看:
if(isdigit(c)) {
Logger::Instance()->write((char) c);
Logger::Instance()->writeChar(c);
} else if(isalpha(c)) {
if(!shift)
Logger::Instance()->writeChar(tolower((char) c));
else
Logger::Instance()->writeChar((char) c);
Stackoverflow keeps flagging my writing as 'code' and is giving me problems when saving.. sorry for the wait. Stackoverflow一直将我的写作标记为“代码”,并且在保存时给我带来了问题。
I tried the following and it worked fine in Visual C++ 2010 (as I would expect): 我尝试了以下操作,并且在Visual C ++ 2010中正常运行(正如我期望的那样):
#include <iostream>
struct Logger {
template <typename T>
void write(T data) {
std::cout << data;
}
};
int main() {
Logger *log = new Logger;
char c = 't';
log->write(c);
delete log;
}
Perhaps you have some custom-defined operator<<
's that are causing this behavior for you? 也许您有一些自定义的
operator<<
会为您造成这种现象? Could you post a sscce ? 您可以发布一个脚本吗?
Edit: 编辑:
For future reference, here is what would have constituted a SSCCE for your problem (based on your complete code): 供您参考,以下是根据您的问题(根据您的完整代码)构成SSCCE的内容:
#include <iostream>
struct Logger {
template <typename T>
void write(T data) {
std::cout << data;
}
};
int main() {
Logger *log = new Logger;
char c = 't';
log->write(tolower(c));
delete log;
}
The key problem is that the signature of tolower is int tolower(int c)
, that is, it returns an integer, not a character. 关键问题是tolower的签名是
int tolower(int c)
,即它返回一个整数,而不是一个字符。 As such, write<int>
is called, rather than write<char>
. 这样,
write<int>
调用write<int>
,而不是write<char>
。
Please keep in mind how important an sscce is! 请记住sscce有多重要! Your question didn't include the very important bit of code that caused the problem.
您的问题没有包含引起问题的非常重要的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.