C++ getline函数没有';t似乎在c++; ifstream读取; 读。开(名); charg[3]; read.getline(g,3); char v=read.get(); 你能检查一下读数是否正常吗
使用读取时,请在使用值之前始终检查读取是否有效:C++ getline函数没有';t似乎在c++; ifstream读取; 读。开(名); charg[3]; read.getline(g,3); char v=read.get(); 你能检查一下读数是否正常吗,c++,file,C++,File,使用读取时,请在使用值之前始终检查读取是否有效: if (read.getline(g,3)) { // Read worked correctly std::cout << "Got: >" << std::string(g, read.gcount()) << "<\n"; } else { std::cerr << "Read Failed\n"; throw "Failed"; } if ((
if (read.getline(g,3)) {
// Read worked correctly
std::cout << "Got: >" << std::string(g, read.gcount()) << "<\n";
}
else {
std::cerr << "Read Failed\n";
throw "Failed";
}
if ((v = read.get()) != EOF) {
std::cout << "Got: >" << v << "<\n";
}
else {
std::cerr << "Read Failed\n";
throw "Failed";
}
if(read.getline(g,3)){
//阅读正确
std::cout通过std::fstream
读取文件:
std::string str;
std::fstream file = "test.txt";//write in text.txt: GhY67. Test.
while(getline(file, str))//while get line of test.txt file,
// saves
// line in str string.
{
std::cout << str << '\n';
//Outputs str string(all lines in test.txt document)
}
std::string str;
std::fstream file=“test.txt”;//写入text.txt:GhY67.test。
while(getline(file,str))//在获取test.txt文件的行时,
//拯救
//str字符串中的行。
{
std::看不到一些如何使用的示例。仅仅因为您将一个值视为“垃圾”,并不意味着该值没有值。特别是因为返回一个int
,然后存储在char
中。您看到的输出是什么?如果在流式处理时将v
转换为int
,会流化什么值?
std::string str;
while(getline(std::cin, str))//while getline in console, user
//inputs characters and characters in onr line saves in str
//string.*
{
if(str == "What?")
{
std::cout << "Hello, world!";
//*If user inputs string "What?", console outputs
string "Hello, world!*/
}
}