我的C++程序中的文件输入有问题
Having issue with file input in my C++ program
我的代码可以编译,但由于某种原因出现了一个错误:
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;
}
"open" 函数文件名参数具有 char* 数据类型,但是,您正在尝试传递 std::string 类型路径。Open 函数是一个 C 函数,它不知道 std::string 类型。
您必须强制转换/转换 std::字符串到字符*
用;
fileinpt.open(txtfile.c_str());
相关文章:
- C++文本文件输入
- C++ 中文件输入输出中的长长 v/s int
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 用空格填充 2D 数组(从文件输入)
- 流对象来表示文件输入,然后是标准输入?
- 用精神解析文件输入
- 从文件输入和输出
- 来自文本文件输入 c++ 的向量
- 如何通过键盘或分数文件输入分数
- 了解文件输入和输出,我可以写入文件,但无法从文件打印
- 词频程序 - 文件输入太大?
- 尝试从 csv 文件输入大量数据时出错
- 如何将文件从文件输入到C 中的矩阵中
- 如何从Windows CMD运行可执行文件并将其传递给文件输入的参数参数和重定向
- 如何解析文本文件并使用构造函数中的文件输入来创建对象的容器
- 这是读取文件输入的正确方法吗?
- 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员
- 如何创建一个具有空值、递减编号系统和.txt文件输入值的二维阵列网格
- C 文件输入流循环问题
- 使用对输入整数的文件输入