关于C++文件打开问题,高分

来源:百度知道 编辑:UC知道 时间:2024/05/31 18:11:52
#include<iostream>
#include<fstream>
#include<cstdlib>
#include"string"
const int SIZE=50;
int main()
{
using namespace std;

ifstream fin;
fin.open("Example.txt");
if(fin.fail())
{
cout<<"Could not open the file "<<". "<<endl;
exit(EXIT_FAILURE);
}
int n[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
string p;
char ch;

while(fin>>ch)
{
if(ch=='\0')
break;
else if(ch<26&&ch>=0)
n[ch-'a']++;
else if(ch<26&&ch>=0)
n[ch-'A']++;
}
for(int i=0;i<26;++i)
{
cout<<(char)('A'+i)<<"/"<<(char)('a'+i);
cout<<" "<<n[i]<<endl;
}
return 0;
}
此文本路径就是cpp所在的路径了,总是打不开文件,请高手解答

估计是机子设置有了问题,换个机子试一下!

把Example.txt放在工程目录下。不要放到debug目录下,如果还不行,估计是工作目录设置问题,你可以使用全路径,比较"E:\\Example.txt"

改一下,写成这样看看
fin.open("\Example.txt");

ifstream fin("Example.txt",ios::in);
不要fin.open,我们老师是这样说的,我也一直这样用,没错过。