【问题标题】:Having issue with file input in my C++ program我的 C++ 程序中的文件输入有问题
【发布时间】:2016-10-04 16:49:01
【问题描述】:

我的代码可以编译,但由于某种原因出现了一个错误:

Error:Multiple markers at this line - Invalid arguments ' Candidates are: void open(const char *, enum std::_Ios_Openmode) ' - no 
matching function for call to 'std::basic_ifstream::open(std::__cxx11::string&)'

我似乎无法弄清楚为什么会引发此错误。

这是我的代码:

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main(){
     int array_for_numbs[10];
     int numbers[9]={1,2,3,4,5,6,7,8,9};
     int counttarr[9]={0};
      ifstream fileinpt;
       int num, dgt;
        string txtfile;
        cout<<"enter the test file:";
         cin >> txtfile;
         fileinpt.open(txtfile);// this line is where the error pops up :(
          int i=0;
 while (!fileinpt.eof())
 {
      fileinpt >> array_for_numbs[i];
      i=i+1;
 }
fileinpt.close();
 for(int i = 0; i < 10; i++)
 {
      num=array_for_numbs[i];
      do
      {
          dgt=num%10;
          num=num/10;
      }while(num>0);
    for(int i = 0; i < 9; i++)
    {
          if(dgt==numbers[i])
          {
               counttarr[i]=counttarr[i]+1;
          }
      }
  }
    cout<<"Digit \t"<< "Count \t"<<"Frequency "<<endl;
   for(int i = 0; i < 9; i++)
 {
      float frq=(float)counttarr[i]/(float)100;
      cout<<(i+1)<<"\t"<< counttarr[i]<<"\t" <<frq<<endl;
 }
 system("pause");
 return 0;
 }

【问题讨论】:

    标签: c++ file input file-io


    【解决方案1】:

    "open" 函数文件名参数具有 char* 数据类型,但是,您正在尝试传递 std::string 类型路径。 Open 函数是一个不知道 std::string 类型的 C 函数。

    您必须将 std::string 转换/转换为 char*

    使用;

    fileinpt.open(txtfile.c_str());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 2017-11-27
      相关资源
      最近更新 更多