C++_文件输入输出


//对文件words.txt中的 单词 按字典序排序并输出到文件output.txt

//#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {

	//将words.txt文件中的单词读入一个vector
	ifstream WordFile("words.txt");

	string word;//单词用字符型
	vector<string> text;

	//while (getline(WordFile, word)) {
	//while (WordFile.get(word)) {
	while (WordFile >> word) {
		text.push_back(word);
	}

	//排序
	sort(text.begin(), text.end());

	ofstream OutPut("output.txt");//将排序后的vector中的单词逐个写入输出文件output
	for (vector<string>::iterator it = text.begin(); it != text.end(); it++)
		OutPut << *it << "\n"; 
}
C++_文件输入输出_第1张图片

C++_文件输入输出_第2张图片

你可能感兴趣的:(C++_文件输入输出)