解析文件

#include <iostream>
#include <stdio.h>
#include "analys.h"
using namespace std;

#define FGETSMAX 6*1024*1024  //获取一行数据的最大值6M

FILE* m_file=NULL;
typedef struct object
{
        //Ob() {};
        //Ob(char *v, int l)
        //        : value(v)
        //        , length(l)
        //{}
        char* Value;
        int Length;
}Object;
/**
 * note 获取一行数据
 *
 * @param filepath 文件路径
 * @param list 目前先用不着·
 *
 * @return 
 */
//获取
int Parse(char *filepath ,int list)
{

        m_file = fopen(filepath, "r");
        cout<<filepath<<endl;
        if(m_file == NULL) {
                cout<<"file paeh is null"<<endl;
                return -1;
        }

        char* str = new char[FGETSMAX];
        int strLen = FGETSMAX;
        fgets(str,strLen,m_file);
        
        cout<<str<<endl;

        delete str;

        return 0;
}

/**
 * note :切分 字符串
 *
 * @param str: 待分割的字符串
 * @param delim: 分隔符字符串 
 * @param dest: 保存分割后的每个字符串
 * @param pCount: 记录一行中分割所得的字符串个数、也就是所谓的列数
 *
 * @return : 正常返回0,字符串为空或者长度为0,则返回-1
 */
int Segment(char* str,char* delim,char** &dest,int* pCount,Object** ob)
{
        char* tmp;
        *pCount = 0;
        if(NULL == str || 0 == strlen(str)) {
                return -1;
        }

        if(NULL == delim || 0 == strlen(delim)) {
                return -1;
        }

        tmp = strtok(str,delim);
        while(tmp != NULL) {
               /*int j;
               for(j=0; tmp[j] != '\0'; j++) {
                       if(tmp[j] == '\n') {
                               cout<<" break"<<endl;
                               break;
                       }
                       (*dest)[j] = tmp[j];
               }
               (*dest)[j] = '\0';
               */
               int tmpLength = strlen(tmp);
               //if(tmp[tmpLength] == '\n' || tmp[tmpLength] == '\r') {  //判断行尾是回车还是换行,若是,换成'\0'
               //        cout<<"出现换行或者回车"<<endl;
               //        tmp[tmpLength] = '\0';
               //}
               (*ob)->Length = tmpLength;
               (*ob)->Value = tmp;
               memcpy((*dest),tmp,tmpLength);
               (*dest)[tmpLength] = '\0';
               dest++;
               ob++;
               (*pCount)++;
               tmp = strtok(NULL,delim);
        }

        return 0;
}

int LoadData(Object** obj, int dataCnt, char *path)
{
        FILE *fp;
        fp = fopen(path,"a");
        if(fp == NULL) {
                cout<<"open file error"<<endl;
        }
        //fseek(fp,0,SEEK_END);
        static int list = 1;
        fprintf(fp,"第%d行:\n",list++);
        for(int i=0; i<dataCnt; i++) {
                //cout<<"("<<obj[i]->Value<<" ----- "<<obj[i]->Length<<")"<<endl;
                fprintf(fp,"(%s ---- %d)\n",obj[i]->Value,obj[i]->Length);
        }

        fclose(fp);
        
        return 0;
}

int main(int argc,char *argv[])
{
        if(argc != 3) {
                return -1;
        }
        FILE* fp;  
        char* lineBuf = new char[FGETSMAX];
        char* delim = "\t";  //分隔符为:空格  
        int num = 0;    //文件中总的字符串个数  
        int count = 0;  //一行中的字符串个数  
        int i;  
        Object **tmpOb = new Object*[128];
        for( i = 0; i < 128; i++)
        {  
				tmpOb[i] = new Object[128];
        }

        char** dest  = new char*[128];  
        for( i = 0; i < 128; i++)
        {  
				dest[i] = new char[128];  
        }  
           
        char** tmpDest;
        tmpDest = dest;  
        if(fp=fopen(argv[1], "r"))  
        {     
				while(fgets(lineBuf, FGETSMAX, fp) != NULL)  
				{ 
					
						//fgets(lineBuf,FGETSMAX,fp);
						//cout<<"读取的一行:"<<lineBuf<<endl; 
						int lineBufLen = strlen(lineBuf);
						//cout<<"yi hang de length "<<lineBufLen<<"zuihou yi ge char:"<<lineBuf[lineBufLen]<<"##"<<lineBuf[lineBufLen-1]<<"-----"<<endl;
						if(lineBuf[lineBufLen-1] == '\n' || lineBuf[lineBufLen-1] == '\r') {  //判断行尾是回车还是换行,若是,换成'\0'
								//cout<<"出现换行或者回车"<<endl;
								lineBuf[lineBufLen-1] = '\0';
						}

						Segment(lineBuf, delim, tmpDest, &count,tmpOb);  
						num  = num + count;  
						LoadData(tmpOb,count,argv[2]);
				}  
        }  
        fclose(fp);  
        /*               
        for(i= 0; i < num; i++)  
        {  
               printf("(%s ,%d)",tmpOb[i]->Value,tmpOb[i]->Length);
               printf("%s\n",dest[i]);  
        }
        */
        for(int i = 0; i<128; i++)  
        {
                delete []dest[i];  
                delete []tmpOb[i];  
        }  
        delete[]dest; 
        delete[]tmpOb; 
        delete lineBuf;
        //Parse(argv[1]);
        
        return 0;
}
#include <iostream>
#include <stdio.h>
#include "analys.h"
using namespace std;


#define FGETSMAX 6*1024*1024  //获取一行数据的最大值6M


FILE* m_file=NULL;
typedef struct object
{
        //Ob() {};
        //Ob(char *v, int l)
        //        : value(v)
        //        , length(l)
        //{}
        char* Value;
        int Length;
}Object;
/**
 * note 获取一行数据
 *
 * @param filepath 文件路径
 * @param list 目前先用不着·
 *
 * @return 
 */
//获取
int Parse(char *filepath ,int list)
{


        m_file = fopen(filepath, "r");
        cout<<filepath<<endl;
        if(m_file == NULL) {
                cout<<"file paeh is null"<<endl;
                return -1;
        }


        char* str = new char[FGETSMAX];
        int strLen = FGETSMAX;
        fgets(str,strLen,m_file);
        
        cout<<str<<endl;


        delete str;


        return 0;
}


/**
 * note :切分 字符串
 *
 * @param str: 待分割的字符串
 * @param delim: 分隔符字符串 
 * @param dest: 保存分割后的每个字符串
 * @param pCount: 记录一行中分割所得的字符串个数、也就是所谓的列数
 *
 * @return : 正常返回0,字符串为空或者长度为0,则返回-1
 */
int Segment(char* str,char* delim,char** &dest,int* pCount,Object** ob)
{
        char* tmp;
        *pCount = 0;
        if(NULL == str || 0 == strlen(str)) {
                return -1;
        }


        if(NULL == delim || 0 == strlen(delim)) {
                return -1;
        }


        tmp = strtok(str,delim);
        while(tmp != NULL) {
               /*int j;
               for(j=0; tmp[j] != '\0'; j++) {
                       if(tmp[j] == '\n') {
                               cout<<" break"<<endl;
                               break;
                       }
                       (*dest)[j] = tmp[j];
               }
               (*dest)[j] = '\0';
               */
               int tmpLength = strlen(tmp);
               //if(tmp[tmpLength] == '\n' || tmp[tmpLength] == '\r') {  //判断行尾是回车还是换行,若是,换成'\0'
               //        cout<<"出现换行或者回车"<<endl;
               //        tmp[tmpLength] = '\0';
               //}
               (*ob)->Length = tmpLength;
               (*ob)->Value = tmp;
               memcpy((*dest),tmp,tmpLength);
               (*dest)[tmpLength] = '\0';
               dest++;
               ob++;
               (*pCount)++;
               tmp = strtok(NULL,delim);
        }


        return 0;
}


int LoadData(Object** obj, int dataCnt, char *path)
{
        FILE *fp;
        fp = fopen(path,"a");
        if(fp == NULL) {
                cout<<"open file error"<<endl;
        }
        //fseek(fp,0,SEEK_END);
        static int list = 1;
        fprintf(fp,"第%d行:\n",list++);
        for(int i=0; i<dataCnt; i++) {
                //cout<<"("<<obj[i]->Value<<" ----- "<<obj[i]->Length<<")"<<endl;
                fprintf(fp,"(%s ---- %d)\n",obj[i]->Value,obj[i]->Length);
        }


        fclose(fp);
        
        return 0;
}


int main(int argc,char *argv[])
{
        if(argc != 3) {
                return -1;
        }
        FILE* fp;  
        char* lineBuf = new char[FGETSMAX];
        char* delim = "\t";  //分隔符为:空格  
        int num = 0;    //文件中总的字符串个数  
        int count = 0;  //一行中的字符串个数  
        int i;  
        Object **tmpOb = new Object*[128];
        for( i = 0; i < 128; i++)
        {  
tmpOb[i] = new Object[128];
        }


        char** dest  = new char*[128];  
        for( i = 0; i < 128; i++)
        {  
dest[i] = new char[128];  
        }  
           
        char** tmpDest;
        tmpDest = dest;  
        if(fp=fopen(argv[1], "r"))  
        {     
while(fgets(lineBuf, FGETSMAX, fp) != NULL)  


//fgets(lineBuf,FGETSMAX,fp);
//cout<<"读取的一行:"<<lineBuf<<endl; 
int lineBufLen = strlen(lineBuf);
//cout<<"yi hang de length "<<lineBufLen<<"zuihou yi ge char:"<<lineBuf[lineBufLen]<<"##"<<lineBuf[lineBufLen-1]<<"-----"<<endl;
if(lineBuf[lineBufLen-1] == '\n' || lineBuf[lineBufLen-1] == '\r') {  //判断行尾是回车还是换行,若是,换成'\0'
//cout<<"出现换行或者回车"<<endl;
lineBuf[lineBufLen-1] = '\0';
}


Segment(lineBuf, delim, tmpDest, &count,tmpOb);  
num  = num + count;  
LoadData(tmpOb,count,argv[2]);
}  
        }  
        fclose(fp);  
        /*               
        for(i= 0; i < num; i++)  
        {  
               printf("(%s ,%d)",tmpOb[i]->Value,tmpOb[i]->Length);
               printf("%s\n",dest[i]);  
        }
        */
        for(int i = 0; i<128; i++)  
        {
                delete []dest[i];  
                delete []tmpOb[i];  
        }  
        delete[]dest; 
        delete[]tmpOb; 
        delete lineBuf;
        //Parse(argv[1]);
        
        return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值