【问题标题】:C++, reading chars into a vector<char> from a file, character by characterC ++,从文件中逐个字符地将字符读入vector<char>
【发布时间】:2015-12-05 06:00:18
【问题描述】:

我正在尝试将名为“board.txt”的文件的前 7 个字符读入向量,但由于某种原因我遇到了问题。我对 C++ 不太熟悉,所以任何建议都将不胜感激,这是我到目前为止的代码

    //rack
int charCount = 0;
char ch;

ifstream rackIn("board.txt");

while(rackIn.get(ch) && charCount < 7){
    this->getMyRack().push_back(ch);
}

这是上面代码中使用的函数getMyRack:

vector<char> board::getMyRack(){
    return this->myRack;
}

myRack 是一个字符向量

我试图在我的主要测试中使用这个:

for (int i = 0; i < test->getMyRack().size(); ++i){
    cout << test->getMyRack().at(i);
} 

但它没有输出任何东西,为什么我正在读取的字符没有被添加到我的字符向量中?

【问题讨论】:

    标签: c++ vector fstream ifstream


    【解决方案1】:

    因为您没有将 char 放入向量中。您的函数 getMyRack() 返回向量,但不返回向量的地址。您可以在班级板上添加方法以添加字符,例如:

     void board::addChar(char c){
         this->myRack.push_back(c);
       }
    

    然后调用这个函数:

     while(rackIn.get(ch) && charCount < 7){
        this->addChar(ch);   
      }
    

    或者改变你的函数的返回类型。

    【讨论】:

      【解决方案2】:
      std::string str;
         int char_count=0;
          // Read the next line from File untill it reaches the 7.
          while (std::getline(in, str)&& char_count!=7)
          {
              // Line contains string of length > 0 then save it in vector
              if (str.size() > 0)
                  your_char_vector.push_back(str);
                    char_count++;
                 if(char_count==7)
                    break;
          }
      

      【讨论】:

      • 您好!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
      【解决方案3】:
      1. 从文件中读取第一行或(需要多少行)到字符串

      2. 从头开始创建 7 个字符的子字符串

           std::ifstream file("board.txt");
        
           std::string str;
        
            // to read single line
        
            std::getline(file, str);
        
          // to read 7 chars 
        
          str= str.substr(0,7); 
        
          vector<char> char_buf;
        
          for(size_t i =0; i <= str.size();i++)
          {
        
             char_buf.push_back(str[i]) 
        
         }
         // use the char_buf 
        

      更简单或第二种方法是使用

                  #include<fstream> // for ifstream
                    
                  #include <cstdlib> // for exit()
      
                  std::string file_name ="board.txt";
                  
                  std::ifstream input_stream;
                 
                  std::vector<char> char_buf;
                  
                   input_stream.open(file_name); 
      
                  if(input_stream.fail()) { exit(0);}
      
                   int char_no=0;
                  
                 while(i<=7)
                            
                  {
                   char c = input_stream.get();
                    char_buf.push_back(c);
                     i++;              
                   }
                   
                 // use char_buf
                
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-22
        • 2020-07-08
        • 2011-04-22
        • 1970-01-01
        • 2012-08-27
        • 2011-08-01
        • 2019-11-11
        • 1970-01-01
        相关资源
        最近更新 更多