C++ Getline正在将字符串作为字符读取
我有一个文本文件,其中一行信息的结构如下: 制造商型号序列号 我正在尝试使用getline检索信息:C++ Getline正在将字符串作为字符读取,c++,std,getline,C++,Std,Getline,我有一个文本文件,其中一行信息的结构如下: 制造商型号序列号 我正在尝试使用getline检索信息: std::string vendorID; std::ifstream vendFile; vendFile.open(fNameVendID); std::getline(vendFile, vendorID); printf("Info: \t\t%s\n", vendorID); 控制台的输出为: E2^ 我是不是错过了getline的一些东西?看起来它正在将文本文件中的三个不同的“单词
std::string vendorID;
std::ifstream vendFile;
vendFile.open(fNameVendID);
std::getline(vendFile, vendorID);
printf("Info: \t\t%s\n", vendorID);
控制台的输出为:
E2^
我是不是错过了getline的一些东西?看起来它正在将文本文件中的三个不同的“单词”打印成三个字符。
printf%s
修饰符应为类似C的char*
字符串,而不是std::string
printf("Info: \t\t%s\n", vendorID.c_str());
或只是,在标准C++中,忘记<代码> Prtff <代码>:
std::cout << "Info: \t\t" << vendorID << std::endl;
std::cout
std::cout << "Info:\t\t" << vendorID <<std::endl;
%s
格式说明符需要的是char*
,而不是std::string
。您的代码显示未定义的行为。使其printf(“信息:\t\t%s\n”,vendorID.c_str())
@IgorTandetnik为什么要使用printf
?毕竟这是一个C++程序,所以输出到<代码>:STD::CUT应该是答案。
printf("Info: \t\t%s\n", vendorID.c_str());