Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Getline正在将字符串作为字符读取_C++_Std_Getline - Fatal编程技术网

C++ 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的一些东西?看起来它正在将文本文件中的三个不同的“单词

我有一个文本文件,其中一行信息的结构如下:

制造商型号序列号

我正在尝试使用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());