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

/*******************************
作者:David Bao 时间:2012年3月14日
描述:
		6种表情{angry,disgust,fear,sadness,smile,surprise}
		每种表情5个图片
		每个图片Gabor变换出来6个图片
		共180副
		此程序专门针对train集!
功能:将PCA得出的文本数据转成weka所需的arff格式
********************************/
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#define N 180
using namespace std;
void main()
{
	string FileName;
	char name[30];
	
	fstream InFile;
	ofstream OutFile("D:\\train\\train.arff");

	if(OutFile.is_open())
	{
		OutFile<<"@relation 'FER'"<<endl;
		char *ch = new char;
		for(int i=1;i<=600;i++)
		{
			itoa(i,ch,10);
			OutFile<<"@attribute feature"+string(ch)+" real"<<endl;
		}
		OutFile << "@attribute 'class' {angry,disgust,fear,sadness,smile,surprise}"<<endl;
		OutFile << "@data" << endl;
		for(int i=1;i<=N;i++)
		{
			int counter = 0;
			//合成文件路径
			itoa(i,name,10);
			FileName = string("C:\\Users\\user\\Desktop\\Gabor_PCA_train\\")+name;
			FileName += string(".txt");
			cout << FileName.c_str() <<endl;
			string line;
			InFile.open(FileName.c_str(),ios::in);
			while(getline(InFile,line))
			{
				istringstream stream(line);
				string word;
				while(stream >> word)
				{
					counter++;
					if(counter > 100)
					//此处乘以6,得到上面的维数 300,因为Gabor将1个图片弄出来了6个分图,而weka中的样本,每一行表示一幅图
					{
						goto loop;
					}
					OutFile << word.c_str() << ",";
				}
			}
loop:
			InFile.close();
			if(i%6==0)
			{
				switch(i/6)
				{
				case 1:
				case 2:
				case 3:
				case 4:
				case 5:
					OutFile << "angry"<<endl;
					break;
				case 6:
				case 7:
				case 8:
				case 9:
				case 10:
					OutFile << "disgust"<<endl;
					break;
				case 11:
				case 12:
				case 13:
				case 14:
				case 15:
					OutFile << "fear"<<endl;
					break;
				case 16:
				case 17:
				case 18:
				case 19:
				case 20:
					OutFile << "sadness"<<endl;
					break;
				case 21:
				case 22:
				case 23:
				case 24:
				case 25:
					OutFile << "smile"<<endl;
					break;				
				case 26:
				case 27:
				case 28:
				case 29:
				case 30:
					OutFile << "surprise"<<endl;
					break;
				}
			}
		}
	}
	else
	{
		cout << "打开文件失败哇……" <<endl;
	}
	//InFile.close();
	OutFile.close();
	system("pause");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值