
C++ 编程统计一个文本文件中字符的个数
我在这个网页看见了你的解答请问下面几句是什么意思,ifstreamfin(c,ios::binary);fin>>d;for(a=0,b=0;d[a]!='\0';a++...
我在这个网页看见了你的解答
请问下面几句是什么意思,
ifstream fin (c,ios::binary);
fin>>d;
for (a=0,b=0;d[a]!='\0';a++)
{
if (d[a]!=' ')
还有fin 的用法 展开
请问下面几句是什么意思,
ifstream fin (c,ios::binary);
fin>>d;
for (a=0,b=0;d[a]!='\0';a++)
{
if (d[a]!=' ')
还有fin 的用法 展开
5个回答
2015-08-05
展开全部
参考代码如下:
//countch.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
ifstream fin(argv[1]);
if (!fin) {
cout << "Can't open file - " << argv[1]
<< "\nUseage : countch filename" << endl;
return 1;
}
string d;
int count = 0;
while ( getline(fin, d) ) //以行为单位读入文件
count += d.size(); //累计字符数
cout << "\n Number of characters : "<< count << endl;
}
//countch.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
ifstream fin(argv[1]);
if (!fin) {
cout << "Can't open file - " << argv[1]
<< "\nUseage : countch filename" << endl;
return 1;
}
string d;
int count = 0;
while ( getline(fin, d) ) //以行为单位读入文件
count += d.size(); //累计字符数
cout << "\n Number of characters : "<< count << endl;
}
展开全部
这是一个对文件操作的程序,ifstream是一个类,主要是用于读取文件中的数据,它内部有很多的成员函数,此类的构造函数有多种,如下
ifstream::ifstream
ifstream();
ifstream( const char* szName, int nMode = ios::in, int nProt = filebuf::openprot );
ifstream( filedesc fd );
ifstream( filedesc fd, char* pch, int nLength );
而你说的fin只是这个类中构造的一个对象,如果你说解释一下fin的用法,我感觉不如说解释一下ifstream的用法,
ifstream fin (c,ios::binary);//构造一个ifstream 的fin对象,fin对象是将C文件以二进制的方式读入内存;
ifstream::ifstream
ifstream();
ifstream( const char* szName, int nMode = ios::in, int nProt = filebuf::openprot );
ifstream( filedesc fd );
ifstream( filedesc fd, char* pch, int nLength );
而你说的fin只是这个类中构造的一个对象,如果你说解释一下fin的用法,我感觉不如说解释一下ifstream的用法,
ifstream fin (c,ios::binary);//构造一个ifstream 的fin对象,fin对象是将C文件以二进制的方式读入内存;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
额…好久没上知道了…来晚了不好意思…楼上说的已经很详细了…我再做点补充:fin是自己声明的ifstream类的一个对象。ifstream类是文件读取对象,它继承自fstream类(标准文件流)。在使用fin时,你就可以看成跟声明普通变量一样,只不过它的数据类型是一个类~明白?
来自:求助得到的回答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
fstream infile;
infile.open("abc.txt",ios::in|ios::binary);
if(!infile)
{
cout<<"abc.txt can't open.\n";
abort();
}
char cha;
int i;
while(!infile.get(cha))
{
i++;
//infile.put(cha);
}
infile.close();
cout<<i<<endl;
}
#include<fstream.h>
#include<stdlib.h>
void main()
{
fstream infile;
infile.open("abc.txt",ios::in|ios::binary);
if(!infile)
{
cout<<"abc.txt can't open.\n";
abort();
}
char cha;
int i;
while(!infile.get(cha))
{
i++;
//infile.put(cha);
}
infile.close();
cout<<i<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int find_str(const char *s, const char c)
{
while(*s)
{
if(*s == c)
return 0;
s++;
}
return 1;
}
//str为原字符串,dest为统计后的串
int main(int argc, char **argv)
{
char *str, dest[1024] = "";
for(int i = 0, j = 0; str[j]; j++)
if(find_str(dest, str[j]))
dest[i++] = str[j], dest[i] = 0;
printf("%s", dest);
return 0;
}
{
while(*s)
{
if(*s == c)
return 0;
s++;
}
return 1;
}
//str为原字符串,dest为统计后的串
int main(int argc, char **argv)
{
char *str, dest[1024] = "";
for(int i = 0, j = 0; str[j]; j++)
if(find_str(dest, str[j]))
dest[i++] = str[j], dest[i] = 0;
printf("%s", dest);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询