C++问题,文件读取
#include<iostream>#include<fstream>#include<string>usingnamespacestd;voidmain(){ifstr...
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main()
{
ifstream infile;
char filename[20];
cout<<"Please enter the file name: ";
infile.open(filename);
int count=0;
cin.getline(filename,20);
char value;
infile>>value;
while(infile.good())
{
count++;
infile>>value;
}
cout<<count<<" characters in the file!"<<endl;
}
这里的代码有什么问题吗,为什么读取不了文档,while直接被跳过了,不执行!还有open()的参数必须是char的吗,string不行?? 展开
#include<fstream>
#include<string>
using namespace std;
void main()
{
ifstream infile;
char filename[20];
cout<<"Please enter the file name: ";
infile.open(filename);
int count=0;
cin.getline(filename,20);
char value;
infile>>value;
while(infile.good())
{
count++;
infile>>value;
}
cout<<count<<" characters in the file!"<<endl;
}
这里的代码有什么问题吗,为什么读取不了文档,while直接被跳过了,不执行!还有open()的参数必须是char的吗,string不行?? 展开
3个回答
展开全部
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main()
{
ifstream infile;
char filename[20];
cout<<"Please enter the file name: ";
cin.getline(filename,20);//获得文件路径
infile.open(filename);//文件路径,没有create
int count=0;//读取的字符数计数
char value;
infile>>value;//预读
while(infile.good())//char文件
{
count++;//计数
infile>>value;//下一个字符,跳过空白符
}
cout<<count<<" characters in the file!"<<endl;
}
#include<fstream>
#include<string>
using namespace std;
void main()
{
ifstream infile;
char filename[20];
cout<<"Please enter the file name: ";
cin.getline(filename,20);//获得文件路径
infile.open(filename);//文件路径,没有create
int count=0;//读取的字符数计数
char value;
infile>>value;//预读
while(infile.good())//char文件
{
count++;//计数
infile>>value;//下一个字符,跳过空白符
}
cout<<count<<" characters in the file!"<<endl;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询