首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

帮小弟我看看这个程序如何改

2012-02-14 
帮我看看这个程序怎么改你看看我的MAIN函数,怎么修改可以作到动态分配呢也就是说文本字符串长度和整数个数

帮我看看这个程序怎么改
你看看我的MAIN函数,怎么修改可以作到动态分配呢也就是说文本字符串长度和整数个数多少是没有限制的,不能用容器;
int   main(void)
{
    ifstream   infile;
    FILE   *file=NULL;
    int   nCountNum=0;
    int   nTempValue=0;
    int   nStringLeght=0;
    int   nIntegerNumber[10];     //整数个数这里改成不固定个数
    int   cString[127];                 //字符串字节数这里也是改成不固定长度
    char   *pChar;
    string   strMyString= " ";
    string   strTempString= " ";

    infile.open( "myfile01.txt ",ios::in);
    file=fopen( "myfile01.txt ", "a ");//这里是不是不能一起打开啊,不过没办法不知道怎么处理
    nStringLenght   =   ftell(file);
    fclose(file);
    while(!infile.eof())
    {
        infile.getline(cString,nStringLenght);
        strMyString   =   cString;
        nStringLenght=strMyString.length();
        pChar=strtok(cString, ", ");
        while(pChar)
        {
            int   i   =0;
            nTempValue=atoi(pChar);
            nIntegerNumber[i++]=nTempValue;
            pChar=strtok(NULL, ", ");
            nCountNum++;
        }
    }
    infile.close();
    QuickSort(nIntegerNumber,0,nCountNum-1);
    for(int   j=0;j <nCountNum;j++)
    {
        printf( "%i ",nIntegerNumber[j]);
    }
    printf( "\n ");
    return   0;
}

[解决办法]
用2种方式同时打开一个文件,已经是恶梦了。
有容器不用的解决方案就是自己手写一个容器(只提供接口最小子集)。
[解决办法]
int nIntegerNumber[10]; //整数个数这里改成不固定个数
==》
这个改用 vector

int cString[127]; //字符串字节数这里也是改成不固定长度
==》
这个改用 string

热点排行