C++文件流

C++文件流

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main() {
	ifstream ifs;
	ifs.open("main.cpp", ios::in);
	if (!ifs)
	{
		cout << "文件打开失败" << endl;
		return;
	}
	ofstream of;
	of.open("Output.txt", ios::out|ios::trunc);
	if (!of)
	{
		cout << "文件打开失败" << endl;
		return;
	}
	char str;
	int  c_count=0;
	int notNull=0;
	int letterCount=0;
	int letter = 0;
	double AvgCount=0.0;
	while (ifs.get(str))
	{
			cout << str;
			of.put(str);
			c_count++;
			if (str!=10&& str!=0 && str!=13&& str != 32)
			{
				notNull++;
			}
			if ((str >= 65 && str <= 90) || (str >= 97 && str <= 122))
			{
				letterCount++;	
				if (ifs.peek()<65|| (ifs.peek()>90&& ifs.peek()<97)||ifs.peek()>122)
				{
					letter++;
				}
			}				
	}
	cout << endl;
	cout << "字符总数:" << c_count << endl;
	cout << "非空白字符总数:" << notNull << endl;
	cout << "字母总数:" << letterCount << endl;
	cout << "平均单词长度:" << letterCount / letter << endl;
	ifs.close();
	of.close();
}

效果图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44567289/article/details/90631721