Thread: Help with File operations

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    Help with File operations

    Dear All,
    My task is as follows:-
    1. I have to open file :CAN.c

    2. find preprocessor directive

    3. If preprocessor directive is Capital then i have to convert it into Lower

    Ex:- #include<CAN.c>---------------->#include<can.c>

    4. write it into other file

    so for i have done as follows:-


    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc,char **argv)
    {
      const int max=1024;
      int r=0,i;
      FILE *in,*out;
      in=fopen("C:\\Documents and Settings\\syg4kor\\Desktop\\Perl_Programming\\CAN.c","rb");
      out=fopen("C:\\Documents and Settings\\syg4kor\\Desktop\\Perl_Programming\\CAN_CPY.c","wb");
      char buff[max+3];
    
      if (argc!=3)
      {
        r=1;
        printf("usage: lower <src> <dest>\n");
      }
      else if ((in=fopen(argv[1],"rb"))==NULL)
        {
          r=2;
          printf("cannot open input-file!\n");
        }
        else if ((out=fopen(argv[2],"wb"))==NULL) // Output file will be overwritten when exists !!!
          {
            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[i]!=NULL && &buff[i]!='\r' && buff[i]!='\n' && (buff[i]==' ' || buff[i]=='\t');i++);
              if (buff[i]=='#')
              {
                for (i=i+1;i<max && buff[i]!=NULL && buff[i]!='\r' && buff[i]!='\n';i++)
                {
                  buff[i]=tolower(buff[i]);
                }
              }
    
              fputs(buff,out);
    	  }
    
            fclose(out);
          }
    
          fclose(in);
    
      return 0;
    }
    But i am getting following errors in highleghed line:

    1. error C2446: '!=' : no conversion from 'int' to 'char *'

    2. error C2040: '!=' : 'char *' differs in levels of indirection from 'int'

    can anybody help me..actually i am learning C programming using this sight..this sight really very helpfull for learning C..i am thankful to all of you

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc,char **argv)
    {
      const int max=1024;
      int r=0,i;
      FILE *in,*out;
      in=fopen("C:\\Documents and Settings\\syg4kor\\Desktop\\Perl_Programming\\CAN.c","rb");
      out=fopen("C:\\Documents and Settings\\syg4kor\\Desktop\\Perl_Programming\\CAN_CPY.c","wb");
      char buff[max+3];
    
      if (argc!=3)
      {
        r=1;
        printf("usage: lower <src> <dest>\n");
      }
      else if ((in=fopen(argv[1],"rb"))==NULL)
        {
          r=2;
          printf("cannot open input-file!\n");
        }
        else if ((out=fopen(argv[2],"wb"))==NULL) // Output file will be overwritten when exists !!!
          {
            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[i]!=NULL && &buff[i]!='\r' && buff[i]!='\n' && (buff[i]==' ' || buff[i]=='\t');i++);
              if (buff[i]=='#')
              {
                for (i=i+1;i<max && buff[i]!=NULL && buff[i]!='\r' && buff[i]!='\n';i++)
                {
                  buff[i]=tolower(buff[i]);
                }
              }
    
              fputs(buff,out);
          }
    
            fclose(out);
          }
    
          fclose(in);
    
      return 0;
    }
    Can you catch the errors highlighted in RED?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    Thanks manav..i got zero error

    Thanks manav it is working...
    Quote Originally Posted by manav View Post
    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc,char **argv)
    {
      const int max=1024;
      int r=0,i;
      FILE *in,*out;
      in=fopen("C:\\Documents and Settings\\syg4kor\\Desktop\\Perl_Programming\\CAN.c","rb");
      out=fopen("C:\\Documents and Settings\\syg4kor\\Desktop\\Perl_Programming\\CAN_CPY.c","wb");
      char buff[max+3];
    
      if (argc!=3)
      {
        r=1;
        printf("usage: lower <src> <dest>\n");
      }
      else if ((in=fopen(argv[1],"rb"))==NULL)
        {
          r=2;
          printf("cannot open input-file!\n");
        }
        else if ((out=fopen(argv[2],"wb"))==NULL) // Output file will be overwritten when exists !!!
          {
            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[i]!=NULL && &buff[i]!='\r' && buff[i]!='\n' && (buff[i]==' ' || buff[i]=='\t');i++);
              if (buff[i]=='#')
              {
                for (i=i+1;i<max && buff[i]!=NULL && buff[i]!='\r' && buff[i]!='\n';i++)
                {
                  buff[i]=tolower(buff[i]);
                }
              }
    
              fputs(buff,out);
          }
    
            fclose(out);
          }
    
          fclose(in);
    
      return 0;
    }
    Can you catch the errors highlighted in RED?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    dear sir...

    I have rectifed the errors..but i am not getting correct result...

    present:- Output file successufully created in present directory with empty

    required:- Output file should creat as copy of original with

    all Upper preprocessor directives replaced with lower preprocessor directives
    ex:- #include<CAN.h>------>#include<can.h>

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc,char **argv)
    {
      const int max=1024;
      int r=0,i;
      FILE *in,*out;
      in=fopen("C:\\Documents and Settings\\syg4kor\\Desktop\\Perl_Programming\\CAN.c","rb");
      out=fopen("C:\\Documents and Settings\\syg4kor\\Desktop\\Perl_Programming\\CAN_CPY.c","wb");
      char buff[max+3];
    
      if (argc!=3)
      {
        r=1;
        printf("usage: lower <src> <dest>\n");
      }
      else if ((in=fopen(argv[1],"rb"))==NULL)
        {
          r=2;
          printf("cannot open input-file!\n");
        }
        else if ((out=fopen(argv[2],"wb"))==NULL) // Output file will be overwritten when exists !!!
          {
            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[i]!='0' && buff[i]!='\r' && buff[i]!='\n' && (buff[i]==' ' || buff[i]=='\t');i++);
    
              if (buff[i]=='#')
              {
                for (i=i+1;i<max && buff[i]!='0' && buff[i]!='\r' && buff[i]!='\n';i++)
                {
                  buff[i]=tolower(buff[i]);
                }
              }
    
              fputs(buff,out);
    	  }
    
            fclose(out);
          }
    
          fclose(in);
    
      return 0;
    }

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    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 <src> <dest>\n");
        }
        else if ((in=fopen(argv[1],"rb"))==NULL)
        {
            r=2;
            printf("cannot open input-file!\n");
        }
        else if ((out=fopen(argv[2],"wb"))==NULL) // Output file will be overwritten when exists !!!
        {
            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[i] && buff[i]!='\r' && buff[i]!='\n' && (buff[i]==' ' || buff[i]=='\t'); i++);
                if (buff[i]=='#')
                {
                    for (i=i+1;i<max && buff[i] && buff[i]!='\r' && buff[i]!='\n';i++)
                    {
                        buff[i]=tolower(buff[i]);
                    }
                }
    
                fputs(buff,out);
            }
    
            fclose(out);
            fclose(in);
        }
    
        return 0;
    }
    This is your code, can you spot the changes?
    It also works fine!

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    for (i=0; i<max && buff[i] && buff[i]!='\r' && buff[i]!='\n' && (buff[i]==' ' || buff[i]=='\t'); i++);
    This line doesn't do anything except waste CPU time (or is optimized out by the compiler). If you actually want it to do something, you need to get rid of the trailing semicolon and block in what you want to loop through.

    ETA: Actually, it does pretty much ensure that i will not be 0 when you get to if(buff[i] == '#')
    Last edited by rags_to_riches; 04-29-2008 at 05:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM