C++ 值不显示在cout中

C++ 值不显示在cout中,c++,cout,C++,Cout,位于“v=”之后的值不会显示在代码中,如下所示。 让我知道如何解决它 while(getline(ifs,line)) { vector<string> strvec=split(line,','); for(int l=0;l<strvec.size();l++) { cout<<"l="<<l<<"value="<<stoi(strvec.at(l

位于“v=”之后的值不会显示在代码中,如下所示。 让我知道如何解决它

while(getline(ifs,line))
    {
        vector<string> strvec=split(line,',');
        for(int l=0;l<strvec.size();l++)
        {
            cout<<"l="<<l<<"value="<<stoi(strvec.at(l))<<endl;
            unsigned char v;
            v=stoi(strvec.at(l));
            File.write((char *)&v, sizeof(char)); 
            cout<<"v="<<v<<endl;
        }           
    }
while(getline(ifs,line))
{
向量strvec=分割(线',');
for(int l=0;l“v=”不显示在代码中,因为v是无符号字符,无符号字符的cout行为是显示它的ASCII值,当它不可打印时,您将得到这些类型的荒谬问题。因此,您可以将v声明为无符号int,或者执行静态转换,如以下示例所示:

#include <iostream>
#include <vector>
int main()
{
  std::vector<std::string> strvec{std::string("10"),std::string("20")};
  for(int l=0;l<strvec.size();l++)
  {

        std::cout<<"l="<<l<<"value="<<stoi(strvec.at(l))<<std::endl;
        unsigned char v;
        v=stoi(strvec.at(l));
        std::cout<<"v="<<static_cast<unsigned>(v)<<std::endl;
  }
  return 0;
}
#包括
#包括
int main()
{
std::vector strvec{std::string(“10”),std::string(“20”)};
for(int l=0;l“v=”不显示在代码中,因为v是无符号字符,无符号字符的cout行为是显示它的ASCII值,当它不可打印时,您将得到这些类型的荒谬问题。因此,您可以将v声明为无符号int,或者执行静态转换,如以下示例所示:

#include <iostream>
#include <vector>
int main()
{
  std::vector<std::string> strvec{std::string("10"),std::string("20")};
  for(int l=0;l<strvec.size();l++)
  {

        std::cout<<"l="<<l<<"value="<<stoi(strvec.at(l))<<std::endl;
        unsigned char v;
        v=stoi(strvec.at(l));
        std::cout<<"v="<<static_cast<unsigned>(v)<<std::endl;
  }
  return 0;
}
#包括
#包括
int main()
{
std::vector strvec{std::string(“10”),std::string(“20”)};

对于(int l=0;l您试图解析的输入是什么?是否
v
包含可打印字符代码,而不是类似于
0x00
?输出如下。l=122value=32 v=l=123value=32 v=Try
cout32是一个空格,因此它自然不可见?您试图解析的输入是什么?是否
v
包含可打印字符代码,而不是类似于
0x00
?输出如下。l=122value=32v=l=123value=32v=Try
cout32是一个空格,因此它自然不可见。谢谢你的Kapli。这也由你的答案解决。另一方面,二进制文件不能通过添加“file.write((char*)&v,sizeof(char))来输出;。输出文件大小为0。请告诉我如何解决。请在您的问题中添加更多详细信息,例如您在哪里打开文件以及为什么要将此v转换为字符,这样我可以提供更好的答案谢谢您Kapli。这也由您的答案解决。另一方面,通过添加“file.write((char*)&v,sizeof(char))无法输出二进制文件;。输出文件大小为0。请告诉我如何解决。请在您的问题中添加更多详细信息,如在何处打开文件以及为什么将此v转换为字符,这样我可以提供更好的答案