How to insert a string array to a vector

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

  • you can not add a character array to vector like that for the same reason that you can not return it from a function. Instead, create a char pointer, allocate memory for it, copy from file input stream then you can add to vector. This should work, but was not compiled or tested.

    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)
  • If you're going to use a vector, you might as well use string object, so you don't have to worry about memory allocation.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • Thank you very much, stober, BitByBit_Thor.

    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.
  • : Thank you very much, stober, BitByBit_Thor.
    :
    : 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)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories