Thread: C programming

  1. #1
    Registered User jacktibet's Avatar
    Join Date
    Mar 2003
    Posts
    18

    Question C programming

    text file(file.in)
    111111...111111
    000001...110000
    111111...111110
    101000...001010
    ...........................
    ...........................
    101110...100001

    the high is 16 characters
    the width is 16 characters
    i need input a two-dimension array to **data


    char **data;
    int hsize; //hsize =16
    int wsize; //wsize=16
    int yy;
    int i;
    FILE *infile;

    can i look as **data as data[][]?
    can i use

    infile=fopen("file.in","r";
    yy=sizeof(char*)*hsize;
    data=(char**)malloc(yy);
    for(i=0;i<wsize;i++)
    fscanf(infile,&data[i]);

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    infile = fopen( "file.in" , "r" );
    data = malloc( hsize );
    for( i = 0; i < hsize; i++ ) {
        data[i] = malloc( wsize );
        fscanf(infile, "%s", data[i] );
    }
    ...
    for( i = 0; i < hsize; i++ )
        free( data[i] );
    free( data );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User jacktibet's Avatar
    Join Date
    Mar 2003
    Posts
    18
    Thank you a lot.
    can u tell me why to use free()?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Anything you (c|m)alloc, you must free when you're done with it.

    Quzah.

Popular pages Recent additions subscribe to a feed