| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 837 人关注过本帖
标题:帮忙看一下为什么不行?
取消只看楼主 加入收藏
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
结帖率:97.66%
收藏
已结贴  问题点数:20 回复次数:11 
帮忙看一下为什么不行?
我实在不知道问题原因在哪?

图片附件: 游客没有浏览图片的权限,请 登录注册


程序代码:

#include <stdio.h>
#include <windows.h>

#define Max_Size 400000
#define Max_Size1 256

const char *delim1=",";
const char *delim2="|";

int My_Max_x=0;
int My_Max_y=0;
int My_Min_x=0;
int My_Min_y=0;
int ErrorCoord=0;
int parts_retested=0;

struct CoodInfo
{
    int x;
    int y;
    int sn;
    int hw;
    int sw;
    int reTest;
};

//------------------------------------------------------------------------------------------------------------------//
char * delN(char * a)
{
    int l;
    l=strlen(a);
    if(a[l-1]==10)
        a[l-1]='\0';
    return a;
}
//------------------------------------------------------------------------------------------------------------------//
int spritcount(char *strIn, const char *aaa)//計算字串中有多少分割符號
{
    int a = 0;
    unsigned int i=0;

    for(i=0; i<strlen(strIn); i++)
    {
        if (strIn[i] == aaa[0])
            a++;
    }
    return(a+1);    //符號數量+1=段落
}
//------------------------------------------------------------------------------------------------------------------//
void strSprit(char *strIn, char **strOut, const char *delim, int nSpritCount)
{
    char *temp;
    int j=0;
    temp = strtok(strIn, delim);
    while( j < nSpritCount )
    {
        strOut[j] = temp;
        j++;
        temp = strtok(NULL, delim);
    }
}
//------------------------------------------------------------------------------------------------------------------//
int CheckCood(int ICCount,CoodInfo *Cood1)
{
    int i=0,x_Max=0,y_Max=0,x_Min=0,y_Min=0;
    int New_x_Max=0,New_y_Max=0,New_x_Min=0,New_y_Min=0;
    
    for (i=0;i<ICCount;i++)
    {
        if(i==0)
        {
            x_Max=Cood1[i].x;x_Min=Cood1[i].x;
            y_Max=Cood1[i].y;y_Min=Cood1[i].y;
        }
        else
        {
            if ((Cood1[i].x>x_Max) || (Cood1[i].y>y_Max))
            {
                if (Cood1[i].x>x_Max)
                    x_Max=Cood1[i].x;
                if (Cood1[i].y>y_Max)
                    y_Max=Cood1[i].y;
            }
            else if ((Cood1[i].x<x_Min) || (Cood1[i].y<y_Min))
            {
                if (Cood1[i].x<x_Min)
                    x_Min=Cood1[i].x;
                if (Cood1[i].y<y_Min)
                    y_Min=Cood1[i].y;
            }
        }
    }
    New_x_Max=((x_Max/10)+1)*10;New_x_Min=((x_Min/10)-1)*10;
    New_y_Max=((y_Max/10)+1)*10;New_y_Min=((y_Min/10)-1)*10;
    My_Max_x=New_x_Max;My_Min_x=New_x_Min;
    My_Max_y=New_y_Max;My_Min_y=New_y_Min;

    if ((x_Min<0)||(y_Min<0)||(x_Min>1000)||(y_Min>1000))
        return -1;
    else
        return 1;
}
//------------------------------------------------------------------------------------------------------------------//
int OutBinMap(char *FileName,char *TargetFile1,char *TargetFile2,int ICCount)
{
    FILE *stream1;
    char Temp[Max_Size]={0},CodeString[Max_Size]={0};
    int i=0,j=0,k=0,ii=0,jj=0,len=0,Title=0,Paragraph=0;

    CoodInfo *Cood = (CoodInfo*)calloc(ICCount,sizeof(CoodInfo));

    if(Cood==NULL)
        return 0;
    
    //------------------------------------------------------------------------------------------收集資料

    stream1=fopen(FileName,"r");

    while(fgets(Temp, Max_Size, stream1)!=NULL)
    {
        ii++;
        if (ii==1)
        {}
        if (ii==2)
        {}
        if (ii==3)
        {
            char *temp="";
            char **part = (char**)malloc(sizeof(char*)*ICCount);
            for(i=0;i<ICCount;i++)
                part[i] = (char*)malloc(sizeof(char)*Max_Size1);

            strcpy(CodeString,delN(Temp));
            len=strlen(CodeString);

            Title = spritcount(CodeString, delim1);                    //依delim1分段落
            temp = strtok(CodeString, delim1);                        //第一次分割(part[0],part[1])
            while( j < ICCount )
            {
                sprintf(part[j] ,"%s", temp);
                j++;
                temp = strtok(NULL, delim1);
            }
            temp="";j=0;
            char *part1[5]={0};
            for (i=1;i<ICCount;i++)
            {
                Paragraph = spritcount(part[i], delim2);            //依delim2分段落
                strSprit(part[i], part1, delim2, Paragraph);        //第二次分割(part[0]....part[Paragraph])
                
                if((part1[0])&&(part1[1])&&(part1[2]))
                {
                    Cood[i-1].x = atoi(part1[0]);
                    Cood[i-1].y = atoi(part1[1]);
                    Cood[i-1].sn = atoi(part1[2]);
                }
            }
            for(i=0;i<Max_Size1;i++)
                free(part[i]);
            free(part);
        }
        else if (ii==4)
        {
            char *temp="";
            char **part = (char**)malloc(sizeof(char*)*ICCount);
            for(i=0;i<ICCount;i++)
                part[i] = (char*)malloc(sizeof(char)*Max_Size1);

            strcpy(CodeString,delN(Temp));
            len=strlen(CodeString);

            Title = spritcount(CodeString, delim1);                    //依delim1分段落
            temp = strtok(CodeString, delim1);                        //第一次分割(part[0],part[1])
            while( j < ICCount )
            {
                sprintf(part[j] ,"%s", temp);
                j++;
                temp = strtok(NULL, delim1);
            }
            temp="";j=0;
            char *part1[5]={0};
            for (i=1;i<ICCount;i++)
            {
                Paragraph = spritcount(part[1], delim2);            //依delim2分段落
                strSprit(part[i], part1, delim2, Paragraph);        //第二次分割(part[0]....part[Paragraph])
                
                if((part1[0])&&(part1[1]))
                {
                    Cood[i-1].hw = atoi(part1[0]);
                    Cood[i-1].sn = atoi(part1[1]);
                }
            }
            for(i=0;i<Max_Size1;i++)
                free(part[i]);
            free(part);
        }
        else if (ii>4)
            break;

        memset(CodeString,0,sizeof(CodeString));
        memset(Temp,0,sizeof(Temp));
    }
    fclose(stream1);
    //------------------------------------------------------------------------------------------
    if(CheckCood(ICCount,Cood)==1)
    {
        char ***NewCood = NULL;
        NewCood = (char***)malloc(sizeof(char**)*My_Max_x);
        for(i=0;i<My_Max_x;i++)
        {
            NewCood[i] = (char**)malloc(sizeof(char*)*My_Max_y);
            for(j=0;j<My_Max_y;j++)
                NewCood[i][j] = (char*)malloc(sizeof(char)*2);
        }
        for(i=0;i<My_Max_x;i++)
            for(j=0;j<My_Max_y;j++)
                for(k=0;k<3;k++)
                    NewCood[i][j][k]='\0';
        
        for(i=0;i<ICCount;i++)
        {
            char tmp1[2]={0};
            itoa(Cood[i].hw,tmp1,10);
            if (strlen(NewCood[Cood[i].x][Cood[i].y])==0)
                Cood[i].reTest = 0;
            else
            {
                Cood[i].reTest = 1;
                parts_retested++;
            }
            strcpy(NewCood[Cood[i].x][Cood[i].y],tmp1) ;
        }
        stream1 = fopen(TargetFile1, "a+" );//HW
        for(int num0=0;num0<3;num0++)
        {
            fprintf(stream1,",");
            for(int num1=0;num1<My_Max_x;num1++)
            {
                if (num0==0)
                    fprintf(stream1,"0,");
                else if (num0==1)
                {
                    int a=num1/10;
                    fprintf(stream1,"%d,",a);
                }
                else if (num0==2)
                {
                    int a=num1%10;
                    fprintf(stream1,"%d,",a);
                }
            }
            fprintf(stream1,"\n");
        }
        for(i=0;i<My_Max_y;i++)
        {
            char *MyString1=NULL;jj=0;
            MyString1 = (char*)malloc(sizeof(char*)*(My_Max_x+5));
            for(int ii=0;ii<My_Max_x;ii++)
            {
                if(strcmp(NewCood[ii][i],"000")>0)
                    jj+=sprintf(MyString1+jj,"%s,",NewCood[ii][i]);
                else
                    jj+=sprintf(MyString1+jj,",");
            }
            fprintf(stream1,"%d,%s\n",i,MyString1);
            MyString1=NULL;
        }
        fprintf(stream1,",\n,\n,\n");
        //ReTest IC
        for(i=0;i<ICCount;i++)
        {
            if (i==0)
                fprintf(stream1,"%s,%s,%s,%s\n","X","Y","Site","HW");
            if (Cood[i].reTest ==1)
                fprintf(stream1,"%d,%d,%d,%d\n",Cood[i].x,Cood[i].y,Cood[i].sn,Cood[i].hw);
        }
        fclose(stream1);

        for(i=0;i<My_Max_x;i++)
        {
            for(j=0;j<My_Max_y;j++)
            {
                for(k=0;k<3;k++)
                    free(NewCood[i][j]);
            }
            free(NewCood[i]);
        }
        free(NewCood);
        NewCood = NULL;
    }
    else
        return 0;
    //------------------------------------------------------------------------------------------
    if(CheckCood(ICCount,Cood)==1)
    {
        char ***NewCood = NULL;
        NewCood = (char***)malloc(sizeof(char**)*My_Max_x);
        for(i=0;i<My_Max_x;i++)
        {
            NewCood[i] = (char**)malloc(sizeof(char*)*My_Max_y);
            for(j=0;j<My_Max_y;j++)
                NewCood[i][j] = (char*)malloc(sizeof(char)*2);
        }
        for(i=0;i<My_Max_x;i++)
            for(j=0;j<My_Max_y;j++)
                for(k=0;k<3;k++)
                    NewCood[i][j][k]='\0';
        
        for(i=0;i<ICCount;i++)
        {
            char tmp1[2]={0};
            itoa(Cood[i].sw,tmp1,10);
            if (strlen(NewCood[Cood[i].x][Cood[i].y])==0)
                Cood[i].reTest = 0;
            else
            {
                Cood[i].reTest = 1;
                parts_retested++;
            }
            strcpy(NewCood[Cood[i].x][Cood[i].y],tmp1) ;
        }
        stream1 = fopen(TargetFile2, "a+" );//HW
        for(int num0=0;num0<3;num0++)
        {
            fprintf(stream1,",");
            for(int num1=0;num1<My_Max_x;num1++)
            {
                if (num0==0)
                    fprintf(stream1,"0,");
                else if (num0==1)
                {
                    int a=num1/10;
                    fprintf(stream1,"%d,",a);
                }
                else if (num0==2)
                {
                    int a=num1%10;
                    fprintf(stream1,"%d,",a);
                }
            }
            fprintf(stream1,"\n");
        }
        for(i=0;i<My_Max_y;i++)
        {
            char *MyString1=NULL;jj=0;
            MyString1 = (char*)malloc(sizeof(char*)*(My_Max_x+5));
            for(int ii=0;ii<My_Max_x;ii++)
            {
                if(strcmp(NewCood[ii][i],"000")>0)
                    jj+=sprintf(MyString1+jj,"%s,",NewCood[ii][i]);
                else
                    jj+=sprintf(MyString1+jj,",");
            }
            fprintf(stream1,"%d,%s\n",i,MyString1);
            MyString1=NULL;
        }
        fprintf(stream1,",\n,\n,\n");
        //ReTest IC
        for(i=0;i<ICCount;i++)
        {
            if (i==0)
                fprintf(stream1,"%s,%s,%s,%s\n","X","Y","Site","SW");
            if (Cood[i].reTest ==1)
                fprintf(stream1,"%d,%d,%d,%d\n",Cood[i].x,Cood[i].y,Cood[i].sn,Cood[i].sw);
        }
        fclose(stream1);

        for(i=0;i<My_Max_x;i++)
        {
            for(j=0;j<My_Max_y;j++)
            {
                for(k=0;k<3;k++)
                    free(NewCood[i][j]);
            }
            free(NewCood[i]);
        }
        free(NewCood);
        NewCood = NULL;
    }
    else
        return 0;

    free(Cood);

    return 1;
}

void main(int argc, char* argv[])
{
    OutBinMap("C:\\TMB\\Transform\\AAAA.csv","C:\\TMB\\Transform\\HW.csv","C:\\TMB\\Transform\\SW.csv",12355);
}
2015-03-17 10:20
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
这是继上次组合出的AAAA.CSV做读取的程式
一做内存释放就当机

[ 本帖最后由 wube 于 2015-3-17 10:22 编辑 ]

不要選我當版主
2015-03-17 10:21
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
不做释放那段的话,基本上运行结果几乎是OK,只是要学习释放加写进去后就卡到阴了.

不要選我當版主
2015-03-17 10:41
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
其实还有一个卡到阴的地方,就是我明明区域声明动态阵列,并且有释放了,但是下一个if的时候再一次使用,
居然当机了,必须得把阵列名称改为另一个才行,这不就又违反变数生命周期的定律了,不过也许是我只释放
内存空间而没把变数设为NULL才会这样?是这样吗?

P.S part[]的地方

不要選我當版主
2015-03-17 11:44
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
以下是引用TonyDeng在2015-3-17 12:06:10的发言:

動態數據在堆上,是歸操作系統管理的。你用malloc()獲得的指針儘管是放在棧上的,函數結束這個指針會自動釋放,但它所指向的數據(在堆上)卻沒被釋放,衹有你用free()告訴操作系統,宣告釋放堆上的內存,操作系統才會在適當的時候擦除那些數據或重用空間,那個時機我們是無法控制的,也就是說,free()之後,數據可能仍在那裏(竊密的經常利用這個原理),也可能不在,説不定。

這就是動態數據的陷阱:真實的數據在堆上,那是永久的全局數據,要麽你主動擦除,要麽讓操作系統替你擦除,釋放掉引用它的指針是沒用的,那反而造成再也無法擦除的後果,因爲再也找不到它們了。一塊堆上的空間,可以有多個指針引用它,隨著程序的複雜,你可能在某個函數中通過一個叫p1的指針用free()釋放堆空間了,但卻在別的函數中不知道指針p2引用那個已被釋放的數據,結果可想而知。這些東西衹有靠程序員自己,從來沒有可靠的編程習慣可以有效預防,否則C++和Java也不會大力鼓吹棄用指針。

如果沒有很好的辦法,乾脆不釋放動態申請的空間,讓操作系統在程序結束時全部抹掉,那在Windows NT內核的系統上獲得保證,不妨如此處理,更省事。


当然在这个范例可以这样做,但是如果养成习惯,日后要编写长驻的程式时,这问题就又会再发生了,所以才趁此机会学习一下标准的写法,
以免造成日后困扰,也可以顺便以后在发生灵异现象时,可以完全排除这部分的疑虑.

关于释放内存空间的部分,虽然有人回答了,也被酸了不少,不过能学到东西就值了,但是仍有些疑问,详细的我先再思考一下要怎发问,再开新帖问好了.

P.S 不是这边的人.

不要選我當版主
2015-03-17 12:34
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
依据网路上教的,确实成功释放内存空间,但是另一个地方,为何第一次成功但第二次又是配不到地址?

图片附件: 游客没有浏览图片的权限,请 登录注册


程序代码:
#include <stdio.h>
#include <windows.h>

#define Max_Size 400000
#define Max_Size1 256

const char *delim1=",";
const char *delim2="|";

int My_Max_x=0;
int My_Max_y=0;
int My_Min_x=0;
int My_Min_y=0;
int ErrorCoord=0;
int parts_retested1=0;
int parts_retested2=0;

struct CoodInfo
{
    int x;
    int y;
    int sn;
    int hw;
    int sw;
    int reTest;
};

//------------------------------------------------------------------------------------------------------------------//
char * delN(char * a)
{
    int l;
    l=strlen(a);
    if(a[l-1]==10)
        a[l-1]='\0';
    return a;
}
//------------------------------------------------------------------------------------------------------------------//
int spritcount(char *strIn, const char *aaa)//計算字串中有多少分割符號
{
    int a = 0;
    unsigned int i=0;

    for(i=0; i<strlen(strIn); i++)
    {
        if (strIn[i] == aaa[0])
            a++;
    }
    return(a+1);    //符號數量+1=段落
}
//------------------------------------------------------------------------------------------------------------------//
void strSprit(char *strIn, char **strOut, const char *delim, int nSpritCount)
{
    char *temp;
    int j=0;
    temp = strtok(strIn, delim);
    while( j < nSpritCount )
    {
        strOut[j] = temp;
        j++;
        temp = strtok(NULL, delim);
    }
}
//------------------------------------------------------------------------------------------------------------------//
int CheckCood(int ICCount,CoodInfo *Cood1)
{
    int i=0,x_Max=0,y_Max=0,x_Min=0,y_Min=0;
    int New_x_Max=0,New_y_Max=0,New_x_Min=0,New_y_Min=0;
    
    for (i=0;i<ICCount;i++)
    {
        if(i==0)
        {
            x_Max=Cood1[i].x;x_Min=Cood1[i].x;
            y_Max=Cood1[i].y;y_Min=Cood1[i].y;
        }
        else
        {
            if ((Cood1[i].x>x_Max) || (Cood1[i].y>y_Max))
            {
                if (Cood1[i].x>x_Max)
                    x_Max=Cood1[i].x;
                if (Cood1[i].y>y_Max)
                    y_Max=Cood1[i].y;
            }
            else if ((Cood1[i].x<x_Min) || (Cood1[i].y<y_Min))
            {
                if (Cood1[i].x<x_Min)
                    x_Min=Cood1[i].x;
                if (Cood1[i].y<y_Min)
                    y_Min=Cood1[i].y;
            }
        }
    }
    New_x_Max=((x_Max/10)+1)*10;New_x_Min=((x_Min/10)-1)*10;
    New_y_Max=((y_Max/10)+1)*10;New_y_Min=((y_Min/10)-1)*10;
    My_Max_x=New_x_Max;My_Min_x=New_x_Min;
    My_Max_y=New_y_Max;My_Min_y=New_y_Min;

    if ((x_Min<0)||(y_Min<0)||(x_Min>1000)||(y_Min>1000))
        return -1;
    else
        return 1;
}
//------------------------------------------------------------------------------------------------------------------//
int OutBinMap(char *FileName,char *TargetFile1,char *TargetFile2,int ICCount)
{
    FILE *stream1;
    char Temp[Max_Size]={0},CodeString[Max_Size]={0};
    int i=0,j=0,k=0,ii=0,jj=0,len=0,Title=0,Paragraph=0;

    CoodInfo *Cood = (CoodInfo*)calloc(ICCount,sizeof(CoodInfo));

    if(Cood==NULL)
        return 0;
    
    //------------------------------------------------------------------------------------------收集資料

    stream1=fopen(FileName,"r");

    while(fgets(Temp, Max_Size, stream1)!=NULL)
    {
        ii++;
        if (ii==1)
        {}
        if (ii==2)
        {}
        if (ii==3)
        {
            char *temp="";
            char **part = (char**)malloc(sizeof(char*)*ICCount);

            if(part==NULL)
                return 0;

            for(i=0;i<ICCount;i++)
                part[i] = (char*)malloc(sizeof(char)*Max_Size1);

            strcpy(CodeString,delN(Temp));
            len=strlen(CodeString);

            Title = spritcount(CodeString, delim1);                    //依delim1分段落
            temp = strtok(CodeString, delim1);                        //第一次分割(part[0],part[1])
            while( j < ICCount )
            {
                sprintf(part[j] ,"%s", temp);
                j++;
                temp = strtok(NULL, delim1);
            }
            temp="";j=0;
            char *part1[5]={0};
            for (i=1;i<ICCount;i++)
            {
                Paragraph = spritcount(part[i], delim2);            //依delim2分段落
                strSprit(part[i], part1, delim2, Paragraph);        //第二次分割(part[0]....part[Paragraph])
                
                if((part1[0])&&(part1[1])&&(part1[2]))
                {
                    Cood[i-1].x = atoi(part1[0]);
                    Cood[i-1].y = atoi(part1[1]);
                    Cood[i-1].sn = atoi(part1[2]);
                }
            }
            for(i=0;i<Max_Size1;i++)
                free(part[i]);
            free(part);
            part=NULL;
        }
        else if (ii==4)
        {
            char *temp="";
            char **part = (char**)malloc(sizeof(char*)*ICCount);

            if(part==NULL)
                return 0;

            for(i=0;i<ICCount;i++)
                part[i] = (char*)malloc(sizeof(char)*Max_Size1);

            strcpy(CodeString,delN(Temp));
            len=strlen(CodeString);

            Title = spritcount(CodeString, delim1);                    //依delim1分段落
            temp = strtok(CodeString, delim1);                        //第一次分割(part[0],part[1])
            while( j < ICCount )
            {
                sprintf(part[j] ,"%s", temp);
                j++;
                temp = strtok(NULL, delim1);
            }
            temp="";j=0;
            char *part1[5]={0};
            for (i=1;i<ICCount;i++)
            {
                Paragraph = spritcount(part[1], delim2);            //依delim2分段落
                strSprit(part[i], part1, delim2, Paragraph);        //第二次分割(part[0]....part[Paragraph])
                
                if((part1[0])&&(part1[1]))
                {
                    Cood[i-1].hw = atoi(part1[0]);
                    Cood[i-1].sn = atoi(part1[1]);
                }
            }
            for(i=0;i<Max_Size1;i++)
                free(part[i]);
            free(part);
            part=NULL;
        }
        else if (ii>4)
            break;

        memset(CodeString,0,sizeof(CodeString));
        memset(Temp,0,sizeof(Temp));
    }
    fclose(stream1);
    //------------------------------------------------------------------------------------------
    if(CheckCood(ICCount,Cood)==1)
    {
        char ***NewCood = NULL;
        NewCood = (char***)malloc(sizeof(*NewCood)*My_Max_x);
        
        if(NewCood==NULL)
            return 0;

        for(i=(My_Max_x-1);i>=0;--i)
        {
            NewCood[i] = (char**)malloc(sizeof(*NewCood[i])*My_Max_y);
            for(j=(My_Max_y-1);j>=0;--j)
                NewCood[i][j] = (char*)malloc(sizeof(*NewCood[i][j])*3);
        }

        for(i=(My_Max_x-1);i>=0;--i)
            for(j=(My_Max_y-1);j>=0;--j)
                for(k=(3-1);k>=0;--k)
                    NewCood[i][j][k]='\0';

        for(i=0;i<ICCount;i++)
        {
            char tmp1[2]={0};
            itoa(Cood[i].hw,tmp1,10);
            if (strlen(NewCood[Cood[i].x][Cood[i].y])==0)
                Cood[i].reTest = 0;
            else
            {
                Cood[i].reTest = 1;
                parts_retested1++;
            }
            strcpy(NewCood[Cood[i].x][Cood[i].y],tmp1) ;
        }
        stream1 = fopen(TargetFile1, "a+" );//HW
        for(int num0=0;num0<3;num0++)
        {
            fprintf(stream1,",");
            for(int num1=0;num1<My_Max_x;num1++)
            {
                if (num0==0)
                    fprintf(stream1,"0,");
                else if (num0==1)
                {
                    int a=num1/10;
                    fprintf(stream1,"%d,",a);
                }
                else if (num0==2)
                {
                    int a=num1%10;
                    fprintf(stream1,"%d,",a);
                }
            }
            fprintf(stream1,"\n");
        }
        
        char *MyString1=NULL;
        MyString1 = (char*)malloc(sizeof(*MyString1)*(My_Max_x+5));

        if(MyString1==NULL)
            return 0;

        for(i=0;i<My_Max_y;i++)
        {
            jj=0;
            memset(MyString1,0,sizeof(MyString1));
            for(int ii=0;ii<My_Max_x;ii++)
            {
                if(strcmp(NewCood[ii][i],"000")>0)
                    jj+=sprintf(MyString1+jj,"%s,",NewCood[ii][i]);
                else
                    jj+=sprintf(MyString1+jj,",");
            }
            fprintf(stream1,"%d,%s\n",i,MyString1);
        }
        fprintf(stream1,",\n,\n,\n");
        MyString1=NULL;

        //ReTest IC
        for(i=0;i<ICCount;i++)
        {
            if (i==0)
                fprintf(stream1,"%s,%s,%s,%s\n","X","Y","Site","HW");
            if (Cood[i].reTest ==1)
                fprintf(stream1,"%d,%d,%d,%d\n",Cood[i].x,Cood[i].y,Cood[i].sn,Cood[i].hw);
        }
        fclose(stream1);

        for(i=0;My_Max_x>i;++i)
        {
            for(j=0;My_Max_y>j;++j)
            {
                free(NewCood[i][j]);
                NewCood[i][j]=NULL;
            }
            free(NewCood[i]);
            NewCood[i]=NULL;
        }
        free(NewCood);
        NewCood = NULL;
    }
    else
        return 0;
    //------------------------------------------------------------------------------------------
    if(CheckCood(ICCount,Cood)==1)
    {
        char ***NewCood = NULL;
        NewCood = (char***)malloc(sizeof(*NewCood)*My_Max_x);

        if(NewCood==NULL)
            return 0;

        for(i=(My_Max_x-1);i>=0;--i)
        {
            NewCood[i] = (char**)malloc(sizeof(*NewCood[i])*My_Max_y);
            for(j=(My_Max_y-1);j>=0;--j)
                NewCood[i][j] = (char*)malloc(sizeof(*NewCood[i][j])*3);
        }
        for(i=(My_Max_x-1);i>=0;--i)
            for(j=(My_Max_y-1);j>=0;--j)
                for(k=(3-1);k>=0;--k)
                    NewCood[i][j][k]='\0';

        for(i=0;i<ICCount;i++)
        {
            char tmp1[2]={0};
            itoa(Cood[i].sw,tmp1,10);
            if (strlen(NewCood[Cood[i].x][Cood[i].y])==0)
                Cood[i].reTest = 0;
            else
            {
                Cood[i].reTest = 1;
                parts_retested2++;
            }
            strcpy(NewCood[Cood[i].x][Cood[i].y],tmp1) ;
        }
        stream1 = fopen(TargetFile2, "a+" );//SW
        for(int num0=0;num0<3;num0++)
        {
            fprintf(stream1,",");
            for(int num1=0;num1<My_Max_x;num1++)
            {
                if (num0==0)
                    fprintf(stream1,"0,");
                else if (num0==1)
                {
                    int a=num1/10;
                    fprintf(stream1,"%d,",a);
                }
                else if (num0==2)
                {
                    int a=num1%10;
                    fprintf(stream1,"%d,",a);
                }
            }
            fprintf(stream1,"\n");
        }

        char *MyString1=NULL;jj=0;
        MyString1 = (char*)malloc(sizeof(*MyString1)*(My_Max_x+5));

        if(MyString1==NULL)
                return 0;

        for(i=0;i<My_Max_y;i++)
        {
            jj=0;
            memset(MyString1,0,sizeof(MyString1));
            for(int ii=0;ii<My_Max_x;ii++)
            {
                if(strcmp(NewCood[ii][i],"000")>0)
                    jj+=sprintf(MyString1+jj,"%s,",NewCood[ii][i]);
                else
                    jj+=sprintf(MyString1+jj,",");
            }
            fprintf(stream1,"%d,%s\n",i,MyString1);
        }
        fprintf(stream1,",\n,\n,\n");
        MyString1=NULL;

        //ReTest IC
        for(i=0;i<ICCount;i++)
        {
            if (i==0)
                fprintf(stream1,"%s,%s,%s,%s\n","X","Y","Site","SW");
            if (Cood[i].reTest ==1)
                fprintf(stream1,"%d,%d,%d,%d\n",Cood[i].x,Cood[i].y,Cood[i].sn,Cood[i].sw);
        }
        fclose(stream1);

        for(i=0;My_Max_x>i;++i)
        {
            for(j=0;My_Max_y>j;++j)
            {
                free(NewCood[i][j]);
                NewCood[i][j]=NULL;
            }
            free(NewCood[i]);
            NewCood[i]=NULL;
        }
        free(NewCood);
        NewCood = NULL;
    }
    else
        return 0;

    free(Cood);
    Cood=NULL;

    return 1;
}

void main(int argc, char* argv[])
{
    int A=OutBinMap("C:\\TMB\\Transform\\AAAA.csv","C:\\TMB\\Transform\\HW.csv","C:\\TMB\\Transform\\SW.csv",12355);
    char B[1]={0};
    itoa(A,B,10);
    MessageBox(NULL,B,"Test Result",MB_OK);
}

不要選我當版主
2015-03-17 15:27
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 

真的很灵异,我改短一点好了。

不要選我當版主
2015-03-17 16:09
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
不是我爱用,因为我只会C,学校只教过C.

不要選我當版主
2015-03-17 18:14
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
单独只选择跑一项都OK,但是跑第一个后再跑第二个,第二个就一定配不到地址,真是很灵异,甚至连变量名称都设成不一样,状况还是一样....

.h
程序代码:
#ifdef __cpluscplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif

EXPORT int OutBinMap(int,char*,char*,int);


.cpp
程序代码:
#include "OutBinMap.h"
#include <stdio.h>
#include <windows.h>

#define Max_Size 400000
#define Max_Size1 256

const char *delim1=",";
const char *delim2="|";

int My_Max_x=0;
int My_Max_y=0;
int My_Min_x=0;
int My_Min_y=0;
int ErrorCoord=0;
int parts_retested1=0;
int parts_retested2=0;
int ICCount=0;

struct CoodInfo
{
    int x;
    int y;
    int sn;
    int hw;
    int sw;
    int reTest;
};

int __stdcall DllMain(HINSTANCE hInstance, DWORD dwReason, PVOID pvReserved)
{
    return TRUE;
}
//------------------------------------------------------------------------------------------------------------------//
char * delN(char * a)
{
    int l;
    l=strlen(a);
    if(a[l-1]==10)
        a[l-1]='\0';
    return a;
}
//------------------------------------------------------------------------------------------------------------------//
int spritcount(char *strIn, const char *aaa)//計算字串中有多少分割符號
{
    int a = 0;
    unsigned int i=0;

    for(i=0; i<strlen(strIn); i++)
    {
        if (strIn[i] == aaa[0])
            a++;
    }
    return(a+1);    //符號數量+1=段落
}
//------------------------------------------------------------------------------------------------------------------//
void strSprit(char *strIn, char **strOut, const char *delim, int nSpritCount)
{
    char *temp;
    int j=0;
    temp = strtok(strIn, delim);
    while( j < nSpritCount )
    {
        strOut[j] = temp;
        j++;
        temp = strtok(NULL, delim);
    }
}
//------------------------------------------------------------------------------------------------------------------//
int CheckCood(int ICCount,CoodInfo *Cood1)
{
    int i=0,x_Max=0,y_Max=0,x_Min=0,y_Min=0;
    int New_x_Max=0,New_y_Max=0,New_x_Min=0,New_y_Min=0;
    
    for (i=0;i<ICCount;i++)
    {
        if(i==0)
        {
            x_Max=Cood1[i].x;x_Min=Cood1[i].x;
            y_Max=Cood1[i].y;y_Min=Cood1[i].y;
        }
        else
        {
            if ((Cood1[i].x>x_Max) || (Cood1[i].y>y_Max))
            {
                if (Cood1[i].x>x_Max)
                    x_Max=Cood1[i].x;
                if (Cood1[i].y>y_Max)
                    y_Max=Cood1[i].y;
            }
            else if ((Cood1[i].x<x_Min) || (Cood1[i].y<y_Min))
            {
                if (Cood1[i].x<x_Min)
                    x_Min=Cood1[i].x;
                if (Cood1[i].y<y_Min)
                    y_Min=Cood1[i].y;
            }
        }
    }
    New_x_Max=((x_Max/10)+1)*10;New_x_Min=((x_Min/10)-1)*10;
    New_y_Max=((y_Max/10)+1)*10;New_y_Min=((y_Min/10)-1)*10;
    My_Max_x=New_x_Max;My_Min_x=New_x_Min;
    My_Max_y=New_y_Max;My_Min_y=New_y_Min;

    if ((x_Min<0)||(y_Min<0)||(x_Min>1000)||(y_Min>1000))
        return 0;
    else
        return 1;
}
//------------------------------------------------------------------------------------------------------------------//
int GenerateHWBinMap(char *TargetFile,CoodInfo *Cood1)
{
    FILE *stream1;
    int i=0,j=0,k=0,jj=0;

    char ***HWCood1 = NULL;
    HWCood1 = (char***)malloc(sizeof(*HWCood1)*My_Max_x);
    
    if(HWCood1==NULL)
        return 0;

    for(i=(My_Max_x-1);i>=0;--i)
    {
        HWCood1[i] = (char**)malloc(sizeof(*HWCood1[i])*My_Max_y);
        for(j=(My_Max_y-1);j>=0;--j)
            HWCood1[i][j] = (char*)malloc(sizeof(*HWCood1[i][j])*3);
    }

    for(i=(My_Max_x-1);i>=0;--i)
        for(j=(My_Max_y-1);j>=0;--j)
            for(k=(3-1);k>=0;--k)
                HWCood1[i][j][k]='\0';

    for(i=0;i<ICCount;i++)
    {
        char tmp1[2]={0};
        itoa(Cood1[i].hw,tmp1,10);
        if (strlen(HWCood1[Cood1[i].x][Cood1[i].y])==0)
            Cood1[i].reTest = 0;
        else
        {
            Cood1[i].reTest = 1;
            parts_retested1++;
        }
        strcpy(HWCood1[Cood1[i].x][Cood1[i].y],tmp1) ;
    }
    stream1 = fopen(TargetFile, "a+" );//HW

    printf("\n %s 處理中 ...\n",TargetFile);

    for(int num0=0;num0<3;num0++)
    {
        fprintf(stream1,",");
        for(int num1=0;num1<My_Max_x;num1++)
        {
            if (num0==0)
                fprintf(stream1,"0,");
            else if (num0==1)
            {
                int a=num1/10;
                fprintf(stream1,"%d,",a);
            }
            else if (num0==2)
            {
                int a=num1%10;
                fprintf(stream1,"%d,",a);
            }
        }
        fprintf(stream1,"\n");
    }
    
    char *MyString1=NULL;
    MyString1 = (char*)malloc(sizeof(*MyString1)*(My_Max_x+5));

    if(MyString1==NULL)
        return 0;

    for(i=0;i<My_Max_y;i++)
    {
        jj=0;
        memset(MyString1,0,sizeof(MyString1));
        for(int ii=0;ii<My_Max_x;ii++)
        {
            if(strcmp(HWCood1[ii][i],"000")>0)
                jj+=sprintf(MyString1+jj,"%s,",HWCood1[ii][i]);
            else
                jj+=sprintf(MyString1+jj,",");
        }
        fprintf(stream1,"%d,%s\n",i,MyString1);
    }
    fprintf(stream1,",\n,\n,\n");
    //free(MyString1);
    MyString1=NULL;

    //ReTest IC
    for(i=0;i<ICCount;i++)
    {
        if (i==0)
            fprintf(stream1,"%s,%s,%s,%s\n","X","Y","Site","HW");
        if (Cood1[i].reTest ==1)
            fprintf(stream1,"%d,%d,%d,%d\n",Cood1[i].x,Cood1[i].y,Cood1[i].sn,Cood1[i].hw);
    }
    fclose(stream1);

    for(i=0;My_Max_x>i;++i)
    {
        for(j=0;My_Max_y>j;++j)
        {
            free(HWCood1[i][j]);
            HWCood1[i][j]=NULL;
        }
        free(HWCood1[i]);
        HWCood1[i]=NULL;
    }
    free(HWCood1);
    HWCood1 = NULL;

    return 1;
}
//------------------------------------------------------------------------------------------------------------------//
int GenerateSWBinMap(char *TargetFile,CoodInfo *Cood1)
{
    FILE *stream1;
    int i=0,j=0,k=0,jj=0;

    char ***SWCood1 = NULL;
    SWCood1 = (char***)malloc(sizeof(*SWCood1)*My_Max_x);

    if(SWCood1==NULL)
        return 0;

    for(i=(My_Max_x-1);i>=0;--i)
    {
        SWCood1[i] = (char**)malloc(sizeof(*SWCood1[i])*My_Max_y);
        for(j=(My_Max_y-1);j>=0;--j)
            SWCood1[i][j] = (char*)malloc(sizeof(*SWCood1[i][j])*3);
    }
    for(i=(My_Max_x-1);i>=0;--i)
        for(j=(My_Max_y-1);j>=0;--j)
            for(k=(3-1);k>=0;--k)
                SWCood1[i][j][k]='\0';

    for(i=0;i<ICCount;i++)
    {
        char tmp1[2]={0};
        itoa(Cood1[i].sw,tmp1,10);
        if (strlen(SWCood1[Cood1[i].x][Cood1[i].y])==0)
            Cood1[i].reTest = 0;
        else
        {
            Cood1[i].reTest = 1;
            parts_retested2++;
        }
        strcpy(SWCood1[Cood1[i].x][Cood1[i].y],tmp1) ;
    }
    stream1 = fopen(TargetFile, "a+" );//SW

    printf("\n %s 處理中 ...\n",TargetFile);

    for(int num0=0;num0<3;num0++)
    {
        fprintf(stream1,",");
        for(int num1=0;num1<My_Max_x;num1++)
        {
            if (num0==0)
                fprintf(stream1,"0,");
            else if (num0==1)
            {
                int a=num1/10;
                fprintf(stream1,"%d,",a);
            }
            else if (num0==2)
            {
                int a=num1%10;
                fprintf(stream1,"%d,",a);
            }
        }
        fprintf(stream1,"\n");
    }

    char *MyString1=NULL;
    MyString1 = (char*)malloc(sizeof(*MyString1)*(My_Max_x+5));

    if(MyString1==NULL)
            return 0;

    for(i=0;i<My_Max_y;i++)
    {
        jj=0;
        memset(MyString1,0,sizeof(MyString1));
        for(int ii=0;ii<My_Max_x;ii++)
        {
            if(strcmp(SWCood1[ii][i],"000")>0)
                jj+=sprintf(MyString1+jj,"%s,",SWCood1[ii][i]);
            else
                jj+=sprintf(MyString1+jj,",");
        }
        fprintf(stream1,"%d,%s\n",i,MyString1);
    }
    fprintf(stream1,",\n,\n,\n");
    //free(MyString1);
    MyString1=NULL;

    //ReTest IC
    for(i=0;i<ICCount;i++)
    {
        if (i==0)
            fprintf(stream1,"%s,%s,%s,%s\n","X","Y","Site","SW");
        if (Cood1[i].reTest ==1)
            fprintf(stream1,"%d,%d,%d,%d\n",Cood1[i].x,Cood1[i].y,Cood1[i].sn,Cood1[i].sw);
    }
    fclose(stream1);

    for(i=0;My_Max_x>i;++i)
    {
        for(j=0;My_Max_y>j;++j)
        {
            free(SWCood1[i][j]);
            SWCood1[i][j]=NULL;
        }
        free(SWCood1[i]);
        SWCood1[i]=NULL;
    }
    free(SWCood1);
    SWCood1 = NULL;

    return 1;
}
//------------------------------------------------------------------------------------------------------------------//
EXPORT int OutBinMap(int Mode,char *FileName,char *TargetFile1,int ICCount1)
{
    FILE *stream1;
    char Temp[Max_Size]={0},CodeString[Max_Size]={0};
    int i=0,j=0,k=0,ii=0,jj=0,len=0,Title=0,Paragraph=0;
    
    ICCount=ICCount1;
    CoodInfo *Cood = (CoodInfo*)calloc(ICCount,sizeof(CoodInfo));

    if(Cood==NULL)
        return 0;

    //------------------------------------------------------------------------------------------收集資料
    stream1=fopen(FileName,"r");
    while(fgets(Temp, Max_Size, stream1)!=NULL)
    {
        ii++;
        if (ii==1)
        {}
        if (ii==2)
        {}
        if (ii==3)
        {
            char *temp="";
            char **part = (char**)malloc(sizeof(char*)*ICCount);

            if(part==NULL)
                return 0;

            for(i=0;i<ICCount;i++)
                part[i] = (char*)malloc(sizeof(char)*Max_Size1);

            strcpy(CodeString,delN(Temp));
            len=strlen(CodeString);

            Title = spritcount(CodeString, delim1);                    //依delim1分段落
            temp = strtok(CodeString, delim1);                        //第一次分割(part[0],part[1])
            while( j < ICCount )
            {
                sprintf(part[j] ,"%s", temp);
                j++;
                temp = strtok(NULL, delim1);
            }
            temp="";j=0;
            char *part1[5]={0};
            for (i=1;i<ICCount;i++)
            {
                Paragraph = spritcount(part[i], delim2);            //依delim2分段落
                strSprit(part[i], part1, delim2, Paragraph);        //第二次分割(part[0]....part[Paragraph])
                
                if((part1[0])&&(part1[1])&&(part1[2]))
                {
                    Cood[i-1].x = atoi(part1[0]);
                    Cood[i-1].y = atoi(part1[1]);
                    Cood[i-1].sn = atoi(part1[2]);
                }
            }
            for(i=0;i<Max_Size1;i++)
                free(part[i]);
            free(part);
            part=NULL;
        }
        else if (ii==4)
        {
            char *temp="";
            char **part = (char**)malloc(sizeof(char*)*ICCount);

            if(part==NULL)
                return 0;

            for(i=0;i<ICCount;i++)
                part[i] = (char*)malloc(sizeof(char)*Max_Size1);

            strcpy(CodeString,delN(Temp));
            len=strlen(CodeString);

            Title = spritcount(CodeString, delim1);                    //依delim1分段落
            temp = strtok(CodeString, delim1);                        //第一次分割(part[0],part[1])
            while( j < ICCount )
            {
                sprintf(part[j] ,"%s", temp);
                j++;
                temp = strtok(NULL, delim1);
            }
            temp="";j=0;
            char *part1[5]={0};
            for (i=1;i<ICCount;i++)
            {
                Paragraph = spritcount(part[1], delim2);            //依delim2分段落
                strSprit(part[i], part1, delim2, Paragraph);        //第二次分割(part[0]....part[Paragraph])
                
                if((part1[0])&&(part1[1]))
                {
                    Cood[i-1].hw = atoi(part1[0]);
                    Cood[i-1].sw = atoi(part1[1]);
                }
            }
            for(i=0;i<Max_Size1;i++)
                free(part[i]);
            free(part);
            part=NULL;
        }
        else if (ii>4)
            break;

        memset(CodeString,0,sizeof(CodeString));
        memset(Temp,0,sizeof(Temp));
    }
    fclose(stream1);
    
    if(CheckCood(ICCount,Cood)==1)
    {
        if (Mode==0)
            if (GenerateHWBinMap(TargetFile1,Cood)!=1)
                return 0;
        if (Mode==1)
            if (GenerateSWBinMap(TargetFile1,Cood)!=1)
                return 0;
    }
    else
        return 0;

    free(Cood);
    Cood=NULL;

    return 1;
}
//------------------------------------------------------------------------------------------------------------------//
/*
void main(int argc, char* argv[])
{
    if (OutBinMap(0,"C:\\TMB\\Transform\\AAAA.csv","C:\\TMB\\Transform\\HW.csv",12355)==1)
        MessageBox(NULL,"OK !","Test Result",MB_OK);
    else
        MessageBox(NULL,"Error !","Test Result",MB_OK);

    if (OutBinMap(1,"C:\\TMB\\Transform\\AAAA.csv","C:\\TMB\\Transform\\SW.csv",12355)==1)
        MessageBox(NULL,"OK !","Test Result",MB_OK);
    else
        MessageBox(NULL,"Error !","Test Result",MB_OK);
}
*/
//------------------------------------------------------------------------------------------------------------------//


摆明是逼我分开写成两个DLL来做

[ 本帖最后由 wube 于 2015-3-17 18:24 编辑 ]

不要選我當版主
2015-03-17 18:22
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
睡夢中,突然周公突然有指示,我明天再改個寫法,說不到問題就在那。

不要選我當版主
2015-03-18 05:35
快速回复:帮忙看一下为什么不行?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040698 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved