Home Forums C Programming Regarding replacing line of code??

Viewing 1 reply thread
  • Author
    Posts
    • #2096
      asfaltinabelle
      Participant

      Dear All,
      My task is:- i have one file i have to identify the preprocessor directives and replace its equavalent lower case if already they are defined with upper case..i have wrote following code..but it is not working could you help me out to solve this?
       


      #include
      #include
      int main(void)
      {
       // open a text file for reading
       FILE * in_file;
       in_file = fopen("C:\Documents and Settings\syg4kor\Desktop\Perl_Programming\CAN.c","r");
      if ( ! in_file  )
      {
      printf ( "file did not openn" );
      return 1;
      }
      int numberOfCharacters = 0;
       char ch = fgetc ( in_file );
       int noc=0;
       char *ptr;
       while ( ch != EOF )
       {
        noc++;
        if(ch=='#')
        {  
         ch++;
         while(ch!='n')
         {
          *ptr=ch;
          ptr++;
          ch++;
         }
        tolower(*ptr);
           }
      ch = fgetc ( in_file );
       
       }
          
           fclose ( in_file );
          return 0;
      }
       

    • #3385
      Priyansh Agrawal
      Participant

      Sorry, your code looks very strange to me. Where do you want to place the output-file ? You opened an input-file for read-only. There you can’t make any changes.

      Perhaps this should help you:


      #include

      int main(int argc,char **argv)
      {
      const int max=1024;
      int r=0,i;
      FILE *in,*out;
      char buff[max+3];

      if (argc!=3)
      {
      r=1;
      printf("usage: lower n");
      }
      else
      {
      if ((in=fopen(argv[1],"rb"))==NULL)
      {
      r=2;
      printf("cannot open input-file!n");
      }
      else
      {
      // Output file will be overwritten when exists !!!
      if ((out=fopen(argv[2],"wb"))==NULL)
      {
      r=3;
      printf("cannot open output-file!n");
      }
      else
      {
      while (!feof(in))
      {
      buff[0]=0;
      fgets(buff,max,in);

      for (i=0;i<max && buff!=0 && buff!='r' && buff!='n' && (buff==' ' || buff=='t');i++);

      if (buff=='#')
      {
      for (i=i+1;i<max && buff!=0 && buff!='r' && buff!='n';i++)
      {
      buff=tolower(buff);
      }
      }

      fputs(buff,out);
      }

      fclose(out);
      }

      fclose(in);
      }
      }

      return r;
      }
Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.