请选择 进入手机版 | 继续访问电脑版

 找回密码
 点一下
查看: 3567|回复: 13

简单的读取并保存slk的一个小程序吧...

[复制链接]
发表于 2008-6-23 00:45:46 | 显示全部楼层 |阅读模式
用标准c++写的...

于是也没有带图形界面...



class SlkTable:

构造函数:
        SlkTable( string file_path)
        参数是slk文件的路径

        构造函数后,文件自动读入

函数:
        const string& get( int x,int y)
        返回x行y列的数据.( y可以比文件中的多,以便于插入数据.数据块最多可以添加ADD_LEN个,默认是10)

        bool set( int x,int y,string str)
        设置x行y列的数据.  注意,str并不需要外面的存储空间,因为内部会自己有一个copy

        int get_length()
        返回数据的总块数,也就是Y的最大值
        int get_size()
        返回数据块的最大行数,也就是X的最大值

        bool save_to_file( string file_path)
        把文件保存到一个文件中.


______________________file SlkTable.h__________________
#ifndef SlkTable_h
#define SlkTable_h


#include "Table.h"

#include<string>
using std::string;

#include<fstream>
using std::ifstream;
using std::ofstream;

class SlkTable
{

        ifstream       reader;                //文件读取流
        enum{ ADD_LEN=10};                    //定义用户最多可以填加多少组数组
        Table<string> table;                  //数据表
        int            length;                //文件的长度
        int            size;                  //文件的数据量

        int            x;                     //当前读取到的x-1
        int            y;                     //当前读取到的y-1
        string         str;                   //本行的数据


public:

        SlkTable( string file_name);
        
        const string& get( int x,int y)
        {
                return table.get( x-1,y-1);
        }
        
        bool set( int x,int y,string str)
        {
                return table.set( x-1,y-1,str);
        }
        
        bool save_to_file( string file_name);

        void get_file_size();
        bool read_line();
        int get_size(){ return size;}
        int get_length(){ return length;}
};

#endif


___________________________file Table.h_____________________
#ifndef Table_h
#define Table_h


#include<vector>
using std::vector;

template<class T>
class Table
{
        T**     data;
        int     length;
        int     size;
        T       t_nil;

public:
        class TableIndexError{};
        Table( int _length=0,int _size=0)
                :length( _length),size( _size)
        {
                data=new T*[size*length];
        }

        T&  get( int x,int y)
        {
                if ( data[y*size+x]==0)
                        return t_nil;
                else
                        return *data[y*size+x];
        }
        bool set( int x,int y,T t)
        {
                data[y*size+x]=new T(t);

        }
};

#endif

_______________________file SlkTable.cpp________________
#include "SlkTable.h"

SlkTable::SlkTable( string file_name)
        : reader( file_name.c_str() )
{
        get_file_size();

        x=0;
        y=0;
        str="";

        table=Table<string>( length+ADD_LEN,size);

        while( read_line() )
        {
                table.set( x,y,str);
        }
        reader.close();
}

void SlkTable::get_file_size()
{
        char c;
        reader>>str;            //无用行

        reader>>c;              //c='B'
        reader>>c;              //c=';'


        reader>>c;              //c='Y'
        reader>>length;
        length+=ADD_LEN;
        reader>>c;              //c=';'

        reader>>c;              //c='X'
        reader>>size;

        reader>>str;
        str="";
}

bool SlkTable::read_line()
{
        char c;
        if ( reader.eof()) return false;

        reader>>c;            //c='C'
        if ( c!='C') return false;


        reader>>c;            //c=';'


        reader>>c;            //c是X或Y


        if ( c=='Y')
        {
                reader>>y;
                y--;

                reader>>c;    //c=';'
                reader>>c;    //c='X'
        }
        //这里,c一定是X了
        reader>>x;
        x--;

        reader>>c;            //c=';'
        reader>>c;            //c='K'
        reader>>str;

        return true;

}

bool SlkTable::save_to_file( string file_name)
{
        ofstream writer( file_name.c_str());

        //header
        writer<<"ID;PWXL;N;E\n";
        //file size& length
        writer<<"B;Y"<<length<<";X"<<size<<";D0\n";
        //data....
        for ( int y=0;y<length;y++)
        {
                bool write_y=false;
                for ( int x=0;x<size;x++)
                {
                        if ( table.get(x,y)=="") continue;
                        if ( write_y==false)
                        {
                                write_y=true;
                                writer<<"C;Y"<<y<<";X"<<x<<";K"<<table.get(x,y)<<"\n";
                        }else
                        {
                                writer<<"C;X"<<x<<";K"<<table.get( x,y)<<"\n";
                        }
                }
        }
        writer<<"E"<<endl;
}

slkeditor.rar

3 KB, 下载次数: 109

 楼主| 发表于 2008-6-23 00:48:21 | 显示全部楼层
不过,上面有一个接口的,可以很容易被图形界面的程序调用..

//对.net己经没有爱了,bcb也有些不爽,于是没有想好,我下一个图形API会去用什么呢...
//不过gtk真的不错..
回复

使用道具 举报

发表于 2008-6-23 17:41:02 | 显示全部楼层
终于放出来了,MB 一下
回复

使用道具 举报

发表于 2008-6-23 17:56:16 | 显示全部楼层
wxwidget。
回复

使用道具 举报

发表于 2008-6-23 18:42:29 | 显示全部楼层
引用第3楼白银の游戏王于2008-06-23 17:56发表的  :
wxwidget。
win下安装wx很麻烦的.
回复

使用道具 举报

发表于 2008-6-23 21:50:44 | 显示全部楼层
觉得编译麻烦就直接下一个binary版。
回复

使用道具 举报

发表于 2008-6-24 12:05:09 | 显示全部楼层
难道就素那个SLKEditor的一部分
回复

使用道具 举报

发表于 2008-7-9 12:26:41 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

发表于 2008-7-11 20:37:38 | 显示全部楼层
我会可塑也不喜欢
还是delphi好啊
回复

使用道具 举报

发表于 2008-7-17 13:59:34 | 显示全部楼层
恩……Delphi确实有过人之处
名字也起的非常好
不然Borland和Microsoft搞了那么多年早就倒了
当年Borland决策失误,幸好有Delphi
回复

使用道具 举报

发表于 2008-7-19 01:37:48 | 显示全部楼层
可是Delphi已被抛弃了
回复

使用道具 举报

发表于 2008-7-19 08:25:10 | 显示全部楼层
所以Delphi是个悲剧工具
回复

使用道具 举报

发表于 2009-7-24 09:05:18 | 显示全部楼层
LZ拿了
回复

使用道具 举报

发表于 2009-7-26 16:25:29 | 显示全部楼层
好个坟。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 点一下

本版积分规则

Archiver|移动端|小黑屋|地精研究院

GMT+8, 2024-3-29 14:47 , Processed in 0.231245 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表