Biggytiny
Biggytiny

Reputation: 529

C++, reading chars into a vector<char> from a file, character by character

I am trying to read in the first 7 chars of a file named "board.txt" into a vector<'char> but I am having issues for some reason. I am not too familiar with C++ so any advice would be appreciated, here is the code I have so far

    //rack
int charCount = 0;
char ch;

ifstream rackIn("board.txt");

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

And here is the function getMyRack used in the code above:

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

myRack is a char vector

I tried to test this in my main using this:

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

but it does not output anything, why are the chars i am reading in not being added into my char vectors?

Upvotes: 0

Views: 1467

Answers (3)

sameraze agvvl
sameraze agvvl

Reputation: 485

  1. read line one or (how much lines required) from file to a string

  2. create substring of 7 chars from beginning

       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 
    

easier or second way is use

            #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
          

Upvotes: 1

user10306682
user10306682

Reputation:

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;
    }

Upvotes: 0

Evgeniy331
Evgeniy331

Reputation: 414

Because you don't put char in your vector. Your function getMyRack() returns vector but not address of your vector. You can add method to your class board for adding char, for example:

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

And then call this function:

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

Or change the return type of your function.

Upvotes: 3

Related Questions