C++ 将char*与要读入文件的字符串协调?

C++ 将char*与要读入文件的字符串协调?,c++,string,char,cin,C++,String,Char,Cin,我正在尝试读取用户的输入,我知道我的方法需要一个字符*但是是否有任何方法可以使cin的输入能够被该字符使用?(请看char*x上的注释。) 字符串y; cout>y; char*x=//这里是字符串的位置。如果我输入实际地址,它会工作,但当用户只是输入地址时,我需要它工作// 字符串行、字符行; ifstreammyfile; myfile.open(x); while(getline(myfile,line)) { 如果(第[0]行!='0'和第[0]行!='1') { 字符线=线; } }

我正在尝试读取用户的输入,我知道我的方法需要一个字符*但是是否有任何方法可以使cin的输入能够被该字符使用?(请看char*x上的注释。)

字符串y;
cout>y;
char*x=//这里是字符串的位置。如果我输入实际地址,它会工作,但当用户只是输入地址时,我需要它工作//
字符串行、字符行;
ifstreammyfile;
myfile.open(x);
while(getline(myfile,line))
{
如果(第[0]行!='0'和第[0]行!='1')
{
字符线=线;
}
}

一个简单的Google会提供结果:)

您只需使用std::string类的
c_str()
方法即可。这项工作:

#include <fstream>
#include <iostream>
#include <string>

int main(void) {
  std::string y;
  std::cout << "Enter your file: ";
  std::cin >> y;
  std::string line,character_line;
  std::ifstream myfile;
  myfile.open (y.c_str(), std::ifstream::in);
  while(getline(myfile,line))
  {
    if (line[0] != '0' && line[0] != '1')
    {   
      character_line = line;
    }   

  }
  return 0;
}
#包括
#包括
#包括
内部主(空){
std::字符串y;
std::cout>y;
std::字符串行、字符行;
std::ifstream myfile;
myfile.open(y.c_str(),std::ifstream::in);
while(getline(myfile,line))
{
如果(第[0]行!='0'和第[0]行!='1')
{   
字符线=线;
}   
}
返回0;
}
使用std::string::c_str()转换为c样式的字符串
char * x = y.c_str();
#include <fstream>
#include <iostream>
#include <string>

int main(void) {
  std::string y;
  std::cout << "Enter your file: ";
  std::cin >> y;
  std::string line,character_line;
  std::ifstream myfile;
  myfile.open (y.c_str(), std::ifstream::in);
  while(getline(myfile,line))
  {
    if (line[0] != '0' && line[0] != '1')
    {   
      character_line = line;
    }   

  }
  return 0;
}