Parse input file in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lucky Beniwal
    New Member
    • Sep 2010
    • 1

    #1

    Parse input file in C

    I need to write a function parse_in with the following prototype :

    int parse_in(FILE *f, int *cost[][]){
    /* parse the input file and fill in const array
    * Need to return the number of cities read in from the file
    * Need to alloca memory for cost
    */
    }


    Using this function, I need to allocate memory for cost, read nxn lines, fill in cost and return n where n is the number of cities.

    I will open the input file, input.txt in main before using this function.

    Can you help me on it..

    The code to open the input file is:

    int main()
    {File *in,*out;
    int i ,n;
    char buffer[100];
    int ch;
    char c;

    in = fopen("input.tx t","r");


    fgets(buffer, 10, in);
    n = atoi(buffer);
    printf("Number of Cities: %d \n\n", n);

    while(!feof(in) )
    {

    c = fgetc(in);
    printf("%c",c);
    }

    fclose(in);
    getchar();
    return 0;

    }


    can you guide me on it please.?
    I need to do this for travelling salesman problem.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    THis is not a function prototype:

    Code:
    int parse_in(FILE *f, int *cost[][]){
    /* parse the input file and fill in const array
    * Need to return the number of cities read in from the file
    * Need to alloca memory for cost
    */
    }
    It's the actual function. It just has a comment for a body.

    All you have to do is replae the comment with tyour calculation.

    This is the function prototype:

    Code:
    int parse_in(FILE *f, int *cost[][]);

    Comment

    Working...