文件处理_保留版_项目中用到

/**********************************************************************
作者:David Bao
时间:2012年3月15日
描述:读取6个表情特征文本,转成两个arff格式文件,输入到weka测试
***********************************************************************/
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#define N 6
using namespace std;
void main()
{
	string FileName;
	char name[30];
	
	fstream InFile;
	ofstream OutFile_train("D:\\1\\W_train.arff");
	if(OutFile_train.is_open())
	{
		cout <<"W_train opened !"<<endl;
	}
	ofstream OutFile_test("D:\\1\\W_test.arff");
	if(OutFile_test.is_open())
	{
		cout << "W_test opened !"<<endl;
	}
//训练集******************************************
		OutFile_train<<"@relation 'FER'"<<endl;
		char *ch1 = new char;
		for(int i=1;i<=300;i++)
		{
			itoa(i,ch1,10);
			OutFile_train<<"@attribute feature"+string(ch1)+" real"<<endl;
		}
		OutFile_train << "@attribute 'class' {angry,disgust,fear,happy,sad,surprise}"<<endl;
		OutFile_train << "@data" << endl;
//测试集******************************************
		OutFile_test<<"@relation 'FER'"<<endl;
		char *ch2 = new char;
		for(int i=1;i<=300;i++)
		{
			itoa(i,ch2,10);
			OutFile_test<<"@attribute feature"+string(ch2)+" real"<<endl;
		}
		OutFile_test << "@attribute 'class' {angry,disgust,fear,happy,sad,surprise}"<<endl;
		OutFile_test << "@data" << endl;
//遍历6个表情文件*************************************
		//6个文件
		for(int i=1; i<=N; i++)
		{
			itoa(i,name,10);
			FileName = string("C:\\Users\\user\\Desktop\\data2\\")+name;
			FileName += string(".txt");
			//cout << FileName.c_str() <<endl;
			InFile.open(FileName.c_str(),ios::in);
			if(InFile.is_open())
			{
				cout <<FileName.c_str()<<"已打开啦!"<<endl;
			}
			//每个文件有600行
			string line;
			int HangHao=0;
			bool flag = true;
			while(getline(InFile,line))
			{
				HangHao++;
				istringstream stream(line);
				string word;
				if(flag)
				{
					while(stream >> word)
					{
						OutFile_train << word.c_str() << ",";
					}
				}
				else
				{
					while(stream >> word)
					{
						OutFile_test << word.c_str() << ",";
					}
				}

				if(HangHao%30==0)
				{
					if(flag)
					{
						switch(i)
						{
						case 1:
							OutFile_train << "angry"<<endl;
							break;
						case 2:
							OutFile_train << "disgust"<<endl;
							break;
						case 3:
							OutFile_train << "fear"<<endl;
							break;
						case 4:
							OutFile_train << "happy"<<endl;
							break;
						case 5:
							OutFile_train << "sad"<<endl;
							break;				
						case 6:
							OutFile_train << "surprise"<<endl;
							break;
						}
					}
					else
					{
						switch(i)
						{
						case 1:
							OutFile_test << "angry"<<endl;
							break;
						case 2:
							OutFile_test << "disgust"<<endl;
							break;
						case 3:
							OutFile_test << "fear"<<endl;
							break;
						case 4:
							OutFile_test << "happy"<<endl;
							break;
						case 5:
							OutFile_test << "sad"<<endl;
							break;				
						case 6:
							OutFile_test << "surprise"<<endl;
							break;
						}
					}
					flag = (!flag);
				}
			}
			InFile.close();
			InFile.clear();
		}

	OutFile_train.close();
	OutFile_test.close();
	system("pause");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值