红色的代码部分是从txt文档读取汉字,为什么蓝色代码部分不能把组合之后的汉字存到txt中去,跪求大神帮忙。
// 组合.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <stdio.h>
using namespace std;
void combinations(char * str, int m, std::vector<string> & chvec){
if(str == NULL || (m != 0 && *str == '\0')) return;
FILE *fp;
fp=fopen("e:\\a.txt","w");//打开文件以便写入数据
if(m == 0){
for(int i = 0; i < chvec.size(); ++i){
std::cout<<chvec[i]<<" ";
fprintf(fp,"%c\n",chvec[i]);
}
std::cout<<std::endl;
return;
}
//选择当前元素
string s(str);
chvec.push_back(s.substr(0,2));
combinations(str+2, m-2, chvec);
chvec.pop_back();
//不选择当前元素
combinations(str+2, m, chvec);
}
void combinations(char * str, int len){
if(str == NULL || len < 1) return;
for(int i = 2; i <= len; ++i){
std::vector<string> chvec;
combinations(str, i, chvec);
}
}
int main(int argc, char ** argv){
char str[100];
fstream out;
out.open("1.txt",ios::in);
cout<<"1.txt"<<" 的内容如下:"<<endl<<endl;
out.getline(str,100,'\n');
cout<<str<<endl<<endl;
cout<<"所有组合如下:"<<endl<<endl;
out.close();
//cin.get();
int len = strlen(str);
combinations(str, len);
system("pause");
return 0;
}