C++,逐个字符地将文件中的字符读入向量<char>
C++, reading chars into a vector<char> from a file, character by character
我正在尝试将名为 "board.txt" 的文件的前 7 个字符读入 vector<'char> 但由于某种原因我遇到了问题。我不太熟悉 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);
}
但它没有输出任何内容,为什么我正在读取的字符没有添加到我的字符向量中?
因为您没有将 char 放入向量中。您的函数 getMyRack()
returns 向量但不是向量的地址。您可以在 class 板上添加方法以添加字符,例如:
void board::addChar(char c){
this->myRack.push_back(c);
}
然后调用这个函数:
while(rackIn.get(ch) && charCount < 7){
this->addChar(ch);
}
或者更改函数的 return 类型。
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;
}
从文件读取第一行或(需要多少行)到字符串
从头开始创建 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
我正在尝试将名为 "board.txt" 的文件的前 7 个字符读入 vector<'char> 但由于某种原因我遇到了问题。我不太熟悉 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);
}
但它没有输出任何内容,为什么我正在读取的字符没有添加到我的字符向量中?
因为您没有将 char 放入向量中。您的函数 getMyRack()
returns 向量但不是向量的地址。您可以在 class 板上添加方法以添加字符,例如:
void board::addChar(char c){
this->myRack.push_back(c);
}
然后调用这个函数:
while(rackIn.get(ch) && charCount < 7){
this->addChar(ch);
}
或者更改函数的 return 类型。
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;
}
从文件读取第一行或(需要多少行)到字符串
从头开始创建 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