By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

Parse input file in C

P: 1
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.txt","r");

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


c = fgetc(in);

return 0;


can you guide me on it please.?
I need to do this for travelling salesman problem.
Sep 17 '10 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
THis is not a function prototype:

Expand|Select|Wrap|Line Numbers
  1. int parse_in(FILE *f, int *cost[][]){
  2. /* parse the input file and fill in const array
  3. * Need to return the number of cities read in from the file
  4. * Need to alloca memory for cost
  5. */
  6. }
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:

Expand|Select|Wrap|Line Numbers
  1. int parse_in(FILE *f, int *cost[][]);
Sep 18 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.