阅读背景:

为什么在代码1中分割错误?

来源:互联网 

Code 1:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

main(int argc,char **argv)
{
    FILE *fp;
    char lineBuf[100],**p=NULL,temp[100];
    int cnt,j,i;
    if(argc!=2)
    {
        puts("ERROR:Invalid no.of arguments");
        return;
    }
    fp=fopen(argv[1],"r");
    if(fp==NULL)
    {
        printf("'%s' file doesn't exist\n",argv[1]);
    }
    cnt=0;

    while(fgets(lineBuf,100,fp))  //...........loop1
    {
        cnt++;
        p=realloc(p,sizeof(char*)*(cnt));
        if(p==NULL)
        {
            puts("memory unavailable");
            return;
        }

        p[cnt-1]=calloc(1,strlen(lineBuf));
        strcpy(p[cnt-1],lineBuf); //................statement1
    }

    fclose(fp);
    for(i=0;i<cnt;i++)
    {
        for(j=i+1;j<cnt;j++)
        {
            if(strcmp(p[i],p[j])>0)
            {
                strcpy(temp,p[i]);
                strcpy(p[i],p[j]);
                strcpy(p[j],temp);
            }
        }
    }

    fp=fopen(argv[1],"w");
    for(i=0;i<cnt;i++)
        fputs(p[i],fp);
    fclose(fp);
    puts("sorting done");
}
#include <stdio.h>
#include <string.h



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: