tags:

views:

150

answers:

2
#define pi 3.142
void main()
{
  float circum,r;
  printf("entr radius");
  scanf("%f",&f);
  area=2*pi*r;
  printf("circum of the circle is =%f",f);
}

We have to write another C program which takes a file as input and replaces the string "pi" with the defined value

Where ever define variable is there i should replace with the value defined by the user.

+1  A: 

void is not a standard return type for main. Use one of the following:

int main(void)

int main(int argc, char *argv[])

You need to read the file, parse each line and see, if it's a macro definition and replace it. Something along the following lines:

int main(int argc, char *argv[]) {
  /* assume: C file to be modified is 1st commandline parameter */
  /* write the modified file to stdout */
  FILE *fp = fopen(argv[ 1 ], "r"); 
  if (fp) {
    char line[ 256 ]; /* change it to something suitable */
    while(fgets(line, sizeof line, fp) != NULL) {
      if (strstr(line, "pi") != NULL) {
         /* parse line, extract input value, change it */
      }
      else puts(line);
    }
    fclose(fp);
  }
  return 0;
}
dirkgently
A: 

Well I will try and guide you in the right direction without doing your homework for you...

In C, main should always return an int. To accept a file as input, you can specify it as a command line argument and read from argv[] or you can use keyboard input for your filename which it appears you already know how to do.

These 3 pages should contain all you will need to finish the task:

John T