tchar 输入输出类 和 string 类 函数简单说明

标准输入输出
printf类
_tprintf : 类同printf
_tprintf_l : 类同printf, 但是"_l"表示第二个参数是一个locale.
locale是操作系统对语言设定的参数, 这个会影响ANSI字符的语言识别, 在Unicode下应该是无差别的
_tprintf_s : 类图printf, 但是和_tprintf相比, _tprintf_s多会做一些检查工作.
如果你的"format string"是动态的, 这个能帮助你.
_tprintf_s_l  : 前两者功能相加
_tprintf_p : 这个用法有点特别, 不过有个例子, 一看就明白
_tprintf_p(TEXT("%1$d times %1$d is %2$d"), 10, 100);
10 times 10 is 100
_tprintf_p_l : _p和_l相加
_tcprintf : 多个一个c, 表示输出到终端~ 要知道标准输入输出不一定是到终端的~
_tcprintf_l _tcprintf_s _tcprintf_p _tcprintf_p_l 就去类比~
_ftprintf : f表示输出到文件
_ftprintf_l ... 去类比
_stprintf : s表示输出到string
_stprintf_l ... 去类比
_sctprintf ... (这里用...表示一堆 _l _p 的函数, 下同)
哈哈, c表示终端, s表示string~ 那到底输出到哪里呢~ 哈哈~
其实_sc表示string count 这个函数不输出~ 只是算算输出的长度~
_tprintf(TEXT("String Length of %d is %d"), 100, _sctprintf(TEXT("%d"), 100));
String Length of 100 is 3
_sntprintf ... : 没啥区别, 有个参数限定输出长度
_v ... ...
_v的太多, 前面的每个函数都有一个_v版本, 它们有个共同特点, 就是最后一个参数是一个特殊的list的指针.

 

printf类有这么多, 来简单总结一下前缀和后缀~
前缀
_t 没的说
_ft 输出到文件
_st 输出到string
_sct 算下长度, 不输出
_snt 输出到string, 还限定长度
_v 用参数的list的指针来表示参数

后缀 
_p 可以用数字表示参数
_l 可以值得locale
_s 多了一些检查

scanf类和printf类类似, 但是没有_v类


剩下来的函数基本上都类似, 大多数能够在c的标准库中找到类似的函数.
每一类中我只举几个典型的函数, _l 和_s后缀是经常可以用的~

 

get 和 put:
_gettc 我要get一个char
_getts 我要get一个string
_puttc, _putts类似

_gettch 我从终端来一个char
_cgetts 我从终端来一个string
_gettche 我从终端来一个char, 顺便输出
_fgettc 我从文件来个char~

string 向数值转换
_tstof string变浮点
_tstol string变长整
_tstoi string变整数
_itot 整数变string

string函数
_tcscat strcat
_tcsncat strncat
_tcscpy strcpy
_tcsncpy strncpy 
_tcslen strlen 
_tcscmp strcmp
_tcsncmp strncmp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是TCHAR和std::string之间的转换方法: 1.将TCHAR转换为std::string: ```c++ #include <string> #include <tchar.h> TCHAR tcharStr[] = _T("TCHAR string"); std::string str = std::string(tcharStr, tcharStr + _tcslen(tcharStr)); ``` 2.将std::string转换为TCHAR: ```c++ #include <string> #include <tchar.h> std::string str = "std::string"; TCHAR tcharStr[100]; _tcscpy_s(tcharStr, CA2T(str.c_str())); ``` 其中,CA2T是ATL/MFC库中的一个宏,用于将const char*转换为TCHAR*。 以下是std::string和LPCWSTR之间的转换方法: 1.将std::string转换为LPCWSTR: ```c++ #include <string> #include <Windows.h> std::string str = "std::string"; LPCWSTR lpcwstr = CA2W(str.c_str()); ``` 其中,CA2W是ATL/MFC库中的一个宏,用于将const char*转换为LPCWSTR。 2.将LPCWSTR转换为std::string: ```c++ #include <string> #include <Windows.h> LPCWSTR lpcwstr = L"LPCWSTR string"; std::wstring wstr(lpcwstr); std::string str(wstr.begin(), wstr.end()); ``` 以下是完整的代码示例: ```c++ #include <iostream> #include <string> #include <tchar.h> #include <Windows.h> using namespace std; int main() { // TCHAR to std::string TCHAR tcharStr[] = _T("TCHAR string"); std::string str1 = std::string(tcharStr, tcharStr + _tcslen(tcharStr)); cout << "TCHAR to std::string: " << str1 << endl; // std::string to TCHAR std::string str2 = "std::string"; TCHAR tcharStr2[100]; _tcscpy_s(tcharStr2, CA2T(str2.c_str())); wcout << "std::string to TCHAR: " << tcharStr2 << endl; // std::string to LPCWSTR std::string str3 = "std::string"; LPCWSTR lpcwstr = CA2W(str3.c_str()); wcout << "std::string to LPCWSTR: " << lpcwstr << endl; // LPCWSTR to std::string LPCWSTR lpcwstr2 = L"LPCWSTR string"; std::wstring wstr(lpcwstr2); std::string str4(wstr.begin(), wstr.end()); cout << "LPCWSTR to std::string: " << str4 << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值