文章目录
C++之文件写入:ifstream、ofstream和fstream
代码
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include <vector>
using namespace std;
class TextUtil
{
public:
// 表格列数
int cow_num;
// 原数据
string data = "";
// 将Tab转化为空格
string convertToSpace = "";
// 分离的单词
vector<string> vocabulary;
// 结果
string result = "";
// 原数据文件
string inpath = "D:\\VSCode\\ConsoleApplication1\\sunshine.txt";
// 生成数据文件
string outpath = "D:\\VSCode\\ConsoleApplication1\\thunder.txt";
ifstream infile;
ofstream outfile;
TextUtil(int cow_num):cow_num(cow_num) {
system(this->inpath.c_str());
this->infile.open(this->inpath);
this->outfile.open(this->outpath);
}
~TextUtil() {
this->infile.close();
this->outfile.close();
system(this->outpath.c_str());
}
// 修改原数据文本路径
void setInpath(const string& str);
// 修改生成数据文本路径
void setOutpath(const string& str);
// 是否打开成功
bool isOpen();
// 录入原数据成一个string字符串:认为单词中不包含空格,之后以空格分割单词
void generateData();
// 将原数据string字符串中的tab换成空格
void generateConvertToSpace();
// 将原数据string字符串中的tab或空格换成#
void generateConvertToSharp();
// 分割的方法
vector<string> split(const string& str, const string& delim);
// 以空格为分割符
void generateVocabulary();
// 录入原数据成一个string字符串:认为每行只包含一个单词,单词中可以包含空格,之后以#分割单词
void generateVocabularyEveryline();
// 写入到生成数据文件中
void writeResult();
//拼接单词
void generateResult1();
//拼接列
void generateResult2();
// 功能封装
void function(int item);
};
void TextUtil::setInpath(const string& str) {
this->inpath = str;
}
void TextUtil::setOutpath(const string& str) {
this->outpath = str;
}
void TextUtil::generateData() {
string line = "";
while (!infile.eof()) {
getline(infile, line);
/*
之所以加""是为了预防:上下行都只有一个单词,而直接拼接成一个单词
*/
this->data = this->data + line + " ";
}
cout << "【原数据】:\n[" << this->data << "]\n\n";
}
void TextUtil::generateVocabularyEveryline(){
string line = "";
while (!infile.eof()) {
getline(infile, line);
int i;
for (i = 0; i < line.length(); i++) {
if (line[i] != ' ' && line[i] != ' '){
break;
}
}
if (i != line.length()) {
this->vocabulary.push_back(line);
}
}
}
void TextUtil::generateConvertToSpace(){
for (int i = 0; i < this->data.length(); i++) {
if (this->data[i] == ' ') {
this->convertToSpace += ' ';
}
else {
this->convertToSpace += this->data[i];
}
}
cout << "【以空格分割的数据】:\n[" << this->convertToSpace << "]\n\n";
}
void TextUtil::generateVocabulary(){
this->vocabulary = this->split(this->convertToSpace," ");
cout << "【已统计单词个数】:\n" << this->vocabulary.size() << endl <<endl;
cout << "【单词详情】:\n";
for (int i = 0; i < this->vocabulary.size(); i++) {
cout << this->vocabulary[i] << endl;
}
cout << endl;
}
void TextUtil::generateResult1(){
this->result += "|";
for (int i = 0; i < this->vocabulary.size(); i++) {
if (i != 0 && (i % this->cow_num) == 0){
this->result += "\n|";
}
this->result = this->result + this->vocabulary[i] + "|";
}
}
void TextUtil::generateResult2(){
int row_num = this->vocabulary.size() / this->cow_num;
this->result += "|";
for (int i = 0; i < row_num; i++){
if (i != 0 ) {
this->result += "\n|";
}
this->result = this->result + this->vocabulary[i] + "|" + this->vocabulary[i+row_num] + "|";
}
}
bool TextUtil::isOpen() {
if (!infile.is_open())
{
cout << "读取失败\n\n";
return false;
}
if (!outfile.is_open())
{
cout << "写入失败\n\n";
return false;
}
return true;
}
void TextUtil::writeResult(){
outfile << this->result;
}
vector<string> TextUtil::split(const string& str, const string& delim) {
vector<string> res;
if ("" == str) return res;
//先将要切割的字符串从string类型转换为char*类型
char* strs = new char[str.length() + 1]; //不要忘了
strcpy(strs, str.c_str());
char* d = new char[delim.length() + 1];
strcpy(d, delim.c_str());
char* p = strtok(strs, d);
while (p) {
string s = p; //分割得到的字符串转换为string类型
res.push_back(s); //存入结果数组
p = strtok(NULL, d);
}
delete[] strs;
delete[] d;
return res;
}
void TextUtil::function(int item){
switch (item) {
/*<1>简单的制表*/
/*原数据
a b c
d e f
*/
/*生成数据
|a|b|c|
|d|e|f|
*/
case 1: {
this->generateData();
this->generateConvertToSpace();
this->generateVocabulary();
this->generateResult1();
break;
}
/*<2>不含空格的合表*/
/*原数据
a
b
c
d
1
2
3
4
*/
/*生成数据
|a|1|
|b|2|
|c|3|
|d|4|
*/
case 2: {
this->generateData();
this->generateConvertToSpace();
this->generateVocabulary();
this->generateResult2();
break;
}
/*<3>含空格的合表*/
/*原数据
a = 3
b
c
d
1 2の 23 3
2
3
4
*/
/*生成数据
|a = 3| 1 2の 23 3|
|b|2|
|c|3|
| d|4|
*/
case 3: {
this->generateVocabularyEveryline();
this->generateResult2();
break;
}
default: {
cout << "没有此选项\n";
break;
}
}
this->writeResult();
}
int main()
{
cout << "输入想要的列数:";
int cow_num ;
cin >> cow_num;
TextUtil textUtil(cow_num);
if (textUtil.isOpen())
{
cout << "请输入想要的功能:\n<1>简单的制表\n<2>不含空格的合表\n<3>含空格的合表\n";
int item;
cin >> item;
textUtil.function(item);
}
return 0;
}