0

I've been at a loss as how to convert the std::string to char (Line 40). I've tried c_str() and a few other things, but it's really confusing when the pointers are thrown in there. I know I'm kind of asking you guys to do it but I don't know what else to do :/.

#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <vector>
#include <new>
#include <string.h>

using namespace std;

bool fileExists(const char *fileName)
{
    ifstream infile(fileName);
    return infile.good();
}

int main ()
{

    vector <string> GNames;
    vector <string> GAliases;

    if ( fileExists("GName.txt") == 0 || fileExists("GAlias.txt") == 0 )
    {

        cout << "Title of 'Name' file should be: GName" << endl;
        cout << "Title of 'Alias' file should be: GAlias" << endl;
        cin.get();

    } else {

        string fileline;
        ifstream tstream("GName.txt", ifstream::in);
        if (tstream.good())
        {
            while (!tstream.eof())
            {
                  getline(tstream, fileline);
                  char * str;
                  str = new char (fileline);
                  char * pch;
                  pch = strtok (str," ");
                  while (pch != NULL)
                  {
                    pch = strtok (NULL, " ");
                    GNames.push_back(pch);
                  }
            }
            tstream.close();
        } else {
            cout << "ERROR 101: Can't open file" << endl;
        }

        cout << "Game generation completed" << endl;
        cin.get();

    }


return 0;
}

Edited by AutoPython

3
Contributors
2
Replies
6
Views
4 Years
Discussion Span
Last Post by rubberman
0

Use the string::c_str() method to extract a const c-string. Also, line 39 should be this:

char * str = ::strdup(fileline.c_str());

and eliminate line 40...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.