user3205160
user3205160

Reputation: 137

How do I convert ifstream or string input to char?

In this method,

void mainRoutine(char *pattern, string searchPattern)
{
        cout << "Please enter the string pattern to be searched: " << endl;
        getline(cin, searchPattern);
        ifstream filename(searchPattern.c_str());
        while(filename.good() && filename.peek() != EOF)
        {
                cout << (char)filename.get();
        }
        cout << "\n";
        char *pattern = (char *)filename;
}

When the user inputs a string - the filename to be taken in, I want to convert this string into a char so I can use it in other methods. How can I do this?

Upvotes: 1

Views: 1188

Answers (2)

user657267
user657267

Reputation: 20990

Just return the std::string

std::string mainRoutine()
{
  std::string searchPattern;
  std::cout << "Please enter the string pattern to be searched: \n";
  std::cin >> searchPattern;

  std::cout << std::ifstream(searchPattern).rdbuf() << '\n';
  // searchPattern.c_str() if not using c++11

  return searchPattern
}

Upvotes: 1

darkboss019
darkboss019

Reputation: 11

char* chFilNam = new char[fileName.length() + 1];
memcpy(chFilNam,fileName.c_str(),fileName.length());

Upvotes: 0

Related Questions