libsvm文件格式修改

搞了一个上午啊,伤不起啊:

还没搞完啊:

  中午有了点思路,何苦保存完数据再这么纠结地修改数据呢?直接在保存源数据的时候,适当修改程序保存成libsvm的格式不就行了吗?

下午从寺里回来,不到10分钟就搞定的,有木有啊?

解决方法:

数字转string函数:

template <class T>
string num_to_string(T N)
{
    strstream ss;
    string s;
    ss << N;
    ss >> s;
    return s;
}

  继续各种转换,但是一个函数内实现:

string CImageDoc::convert_libsvm_txt(int i,double data)
{
	return num_to_string(i)+":"+num_to_string(data)+" ";
}

  ok,就这样就行了,直接调用就搞定啦:

features_file << convert_libsvm_txt(num,d0);++num;
					features_file << convert_libsvm_txt(num,d1);++num;
					features_file << convert_libsvm_txt(num,d2);++num;
					features_file << convert_libsvm_txt(num,d3);++num;

  结果就是libsvm要求的格式:

16 1:569.573 2:743.441 3:5.6058 4:0.00390875 5:0.125238 6:1026.2 7:5.44271 8:0.00464965 9:0.126855 10:1775.25 11:5.49567 12:0.00434028 13:0.144323 14:734.257 15:5.55484 16:0.0039468 17:0.155071 18:1030.29 
66 1:9641.52 2:7216.66 3:8.12506 4:0.000332562 5:0.111952 6:1339.52 7:8.1526 8:0.000319694 9:0.100144 10:1948.68 11:8.13107 12:0.000330743 13:0.113047 14:994.402 15:8.13424 16:0.000324731 17:0.11197 18:1266.74 
0 1:4616.27 2:3289.41 3:7.81044 4:0.000432526 5:0.0986975 6:1640.14 7:7.79356 8:0.000437157 9:0.0870885 10:2161.93 11:7.78078 12:0.00044677 13:0.110117 14:1250.03 15:7.78338 16:0.000441532 17:0.098696 18:1435.79 
1 1:25583 2:24154.1 3:7.58252 4:0.00104005 5:0.214605 6:518.209 7:7.68982 8:0.000874984 9:0.182076 10:711.462 11:7.5777 12:0.00102684 13:0.214733 14:471.483 15:7.65506 16:0.000911101 17:0.192279 18:558.787 

  坛子上wangjunsheng网友给出了代码,非常感谢啊,我还误会他了,纠结啊,再次道歉!

#include <iostream>
#include <string>
using namespace std;

string string_from_filename(const char * filename_)
{
   if (!filename_ || filename_[0] == '\0') return "";
    FILE* fp = fopen( filename_, "rb" );
    if (!fp)
    {
        return "";
    }
    
    // Get file length
    fseek(fp, 0, SEEK_END);
    int len = ftell( fp );
    fseek(fp, 0, SEEK_SET);
    
    string s = "";
    s.resize(len);
    fread(&s[0], len, 1, fp);
    fclose(fp);
    
    return s;
}

void convert(const string& raw, string& o)
{
    const char* s = raw.c_str();
    int cnt = 0;
    bool newline = true;
    char buf[16];
    
    for (; *s; ++s) {
        
        if (newline) {
            if (isdigit(*s)) {
                while (isdigit(*s)) {
                    o += *s;
                    ++s;
                }--s;
                newline = false;    
            } else
                o += *s;
        } else {
            if (isdigit(*s)) {
                ++cnt;
                sprintf(buf, "%d:", cnt);
                o += buf;
                while (isdigit(*s) || *s == '.') {
                    o += *s;
                    ++s;
                }--s;   
            } else {
                o += *s;
                if (*s == '\r' || *s == '\n') {
                    newline = true;
                    cnt = 0;
                }
            }
        }
    }
}

bool save_to_file(const string& filename_, const string& content)
{
    if (filename_.empty()) return false;
    FILE* fp = fopen( filename_.c_str(), "wb+" );
    if (!fp)
    {
        return false;
    }

    size_t len = content.length();

    bool save_success = (len == fwrite(&content[0], 1, len, fp));
//    if (!save_success) msglog("file save failed : %s", filename_.c_str());

    fflush(fp);
    fclose(fp);

    return save_success;
}


void main()
{
    string raw = string_from_filename("新建 文本文档 (2).txt");
    string s;
    convert(raw.c_str(), s);
    save_to_file("new.txt", s);
}

  lvjing_csdn网友提出了更简单的办法:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <exception>
#include <stdexcept>

using namespace std;


int main()
{
	ifstream infile("e:\\test.txt");
	ofstream outfile("e:\\outfile.txt");
	if(!infile || !outfile)
		throw runtime_error("openfile error");
	string line;
	while(getline(infile,line))
	{
		int temp;
		double f;
		istringstream in_line(line);//从当前行中读取数据,亮点啊
		in_line >> temp;
		outfile << temp;
		temp=1;
		while(in_line >> f)
			outfile << " " <<temp++ << ":" << f;
		outfile << endl;
	}
	cout << "complete" << endl;
	return 0;
}

  

你可能感兴趣的:(lib)