2013年1月26日 星期六

[C Language] How to read file and store into 2-D array

[C Language] How to read file and store into 2-D array

int RemoveDuplicate()

{

    size_t i;

    int Flag = 1,len;

    char buf1[BUFF_LENG]={0};

    char *buf2[BUFF_LENG]={0}; // 2-D must announce like this.

  

    FILE *fp1;

    fp1 = fopen(PATH_HTML_PICK, "r");

    if ( fp1 == NULL)

        exit(1);



    i=0;

    while( (fgets(buf1, BUFF_LENG, fp1)!= NULL) )

    {

        buf2[i] = malloc(strlen(buf1)+1);  // assign how much size

        strcpy(buf2[i],buf1); // Copy to buf1 to buf2[0][]

        i++;

    }



    for(i=0;i<15;i++)

    {

        printf("%s",buf2[i]);      

    }

/*

char** get_array_2D(int size) {

    char* string = "Hello World";

    char** abc = malloc(size*sizeof(char**));

    int i = 0;

    for(i;i<size;i++) {

             abc[i] = malloc(strlen(string)+1);

             strcpy(abc[i], string);

    }

    return abc;

}

*/



    fclose(fp1);



  

    return 1;

}

0 意見:

張貼留言