我试图打开一个.dat文件以用作我的程序的输入。该任务说我需要将我输入的文件名称转换为c-string数据类型,以便通过.open(“”)命令读取它。我的程序编译完成,但我确信当我尝试转换文件名时,我做错了什么。我曾四处寻找有类似问题的人,但我没有运气,所以你可以给我的任何建议将非常感激!将文件的名称转换为c字符串
这里是我尝试打开文件的功能,以及我尝试转换文件名的其他功能。
int main()
{
ifstream fp;
string name[SIZE], filename;
int counter, idx = 0;
float rate[SIZE], sum[SIZE], gross[SIZE], with[SIZE], pay[SIZE], net[SIZE], hours[SIZE];
getfile(fp, filename);
readFile(fp, name, rate, hours);
pay[SIZE] = calcPay(rate, sum);
gross[SIZE] = calcGross(pay);
with[SIZE] = calcAmount(gross);
net[SIZE] = calcNet(gross, with);
output(name, rate, sum, with, gross, net, pay, SIZE);
return 0;
}
//Convert filename into C-string
string convert(ifstream &fp, string filename)
{
fp.open(filename.c_str());
return filename;
}
//Get file name from user.
void getfile(ifstream &fp, string filename)
{
cout <<" Enter the name of the file: ";
cin>>filename;
convert(fp, filename);
fp.open("filename");
if (!fp)
{
cout<<"Error opening file\n";
exit (1);
}
}
是什么让你觉得你做错了什么? – 0x499602D2
当你运行程序时会发生什么?如果编译器没有发出抱怨,您的转换必须给出一些合理的结果... – abiessu
当我输入为作业下载的.dat文件的名称时,程序输出“Error opening file”。 – user3063730