Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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函数没有';t似乎在c++; ifstream读取; 读。开(名); charg[3]; read.getline(g,3); char v=read.get(); 你能检查一下读数是否正常吗_C++_File - Fatal编程技术网

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!*/
      }
 }