Hi, guys.
I am trying to insert a string into a vector in C++. But it never works.
Actually, I am new to C++. So, I am not familiar with the vector in C++.
Following is the code.
--------------code--------------
[code]
#include #include using namespace std;
class VecTest
{
public :
VecTest();
~VecTest();
int tokenize();
private :
vector tokens;
};
int VecTest::tokenize()
{
char token_buf[100];
int char_counter = 0;
CFile file;
file.Open(_T("sort.c"), CFile::modeRead);
char one_char;
int counter = 0;
while(counter < 90){
file.Read(&one_char, sizeof(char));
token_buf[char_counter] = one_char;
char_counter++;
}
file.Close();
tokens.push_back(token_buf);
return 0;
}
[/code]
-----------------------------------
#This is part of my code.
When I build this code, the vs said it cannot convert cahr [100] to
const char (&)[1]. I even don't what does it mean.
What i am trying to do is, read char from a file and break sentences into
words and save the words into a vector.
PLZ, HELP. I have to finish this today.
Thank you very much.
Have a nice day.
Comments
Also: you have a bug -- delete variable [b]counter[/b] used in the while loop because it is never incremented and results in an infinite loop.
[code]
int VecTest::tokenize()
{
char* token_buf = new char[100];
int char_counter = 0;
CFile file;
file.Open(_T("sort.c"), CFile::modeRead);
char one_char;
char_counter = 0;
while(char_counter < 90){
file.Read(&one_char, sizeof(char));
token_buf[char_counter] = one_char;
char_counter++;
}
token_buf[char_counter] = 0; // null-terminate the string
file.Close();
tokens.push_back(token_buf);
return 0;
}
[/code]
=============================================
never lie -- the government doesn't like the competition. (Author unknown)
Best Regards,
Richard
The way I see it... Well, it's all pretty blurry
I have solved this problem with your advices. It is too complicate to push
a string into a vector by using char array. There are too many data type
cast problems that i cannot understand.
As BitByBit_Thor said, It is good to use a String object.
Thank you all again.
Best regards.
:
: I have solved this problem with your advices. It is too complicate
: to push
: a string into a vector by using char array. There are too many data
: type
: cast problems that i cannot understand.
:
: As BitByBit_Thor said, It is good to use a String object.
:
: Thank you all again.
:
:
: Best regards.
:
when using a std::string you declare the vector like this:
[code]
vector array;
[/code]
=============================================
never lie -- the government doesn't like the competition. (Author unknown)