Question

The only question i found that tried to help me was this one C++: splitting a string into an array. I'm new to c++ and i need to have an array of string that cointain each of these words that i have in this char.

Here is the code:

s3eFile* file = s3eFileOpen("chatTest/restrict_words.txt","rb"); 
        int len = s3eFileGetSize(file);

        char* temp = new char[len];
        if (file!=NULL)
        {
            s3eFileRead(temp,len,1, file);
            s3eFileClose(file);
        }

So i need to make this temp to turn in to an array so i can work with it? There's a way?

Was it helpful?

Solution 2

If this is a c++ code then i would recommend moving to std::string instead of char* and use powerful std tools like fstream, stringstream etc . The link which you have specified gives detailed answer on how to do it

    #include <string>
    #include <sstream>
    using namespace std;
    .
    .
    .
    s3eFile* file = s3eFileOpen("chatTest/restrict_words.txt","rb"); 
    int len = s3eFileGetSize(file);

    char* temp = new char[len];
    if (file!=NULL)
    {
        s3eFileRead(temp,len,1, file);

        //Adding Code here
        string str(temp);
        stringstream sstr(str)
        vector<string> str_array;
        string extracted;
        while(sstr.good()){
         sstr>>extracted;
         str_array.push_back(extracted);
        }
        //at this point all the strings are in the array str_array

        s3eFileClose(file);
    }

you can access the strings using iterator or by simple indexing like arrays str_array[i]

OTHER TIPS

maybe something like:

ifstream f("chatTest/restrict_words.txt");

vector<string> vec;

while (!f.fail())
{
  string word;
  f >> word; 
  vec.push_back(move(word));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top