Thread: Please Improve my code

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    6

    Lightbulb Please Improve my code

    Hello to every one ....
    I'm a Beginner to programming...
    I have a folder that contains two sub folders one is "JAVA FILES" and another one is CLASS FILES. I write some java programs and compile it and the output of the compilation it generates the .class file . I want to move the .java file to "JAVA FILES" folder and .class file to the "CLASS FILES" folder.So i write a c program to perform this task My c program code is
    Code:
    #include <stdio.h>
    #include <string.h>
    
    //check the file extension is related to class or java files
    int validfile(char * str)
    {
        if(strstr(str,".class") || strstr(str,".java"))
        {
        //    printf("Valid extension");
            return 1;
        }
    
        else
        {
        //    printf("Invalid extension");
            return 0;
        }
        
    }
    
    int main(int argc,char *argv[])
    {
        FILE *Sourcefile,*Destinationfile;
        char filename[50];
            
        if(validfile(argv[1]) == 1)        
        {
        
        //    Open the source file for reading task
            Sourcefile = fopen(argv[1],"r");
    
        //    Check the file is opened without error
            if(Sourcefile == NULL)
            {
                printf("file is not opened something problem in opening file");
                return -1;        
            }    
            
            if(strstr(argv[1],".class"))            
            {
                //char filename[20];
                
                strcpy(filename,".//CLASS FILES//");            
                strcat(filename,argv[1]);        
            //      printf("%s",filename);
            }
            else 
            {
                //char filename[20];
                
                strcpy(filename,".//JAVA FILES//");            
                strcat(filename,argv[1]);        
            //      printf("%s",filename);
                
            }
            
            //     create the destination file 
                Destinationfile = fopen(filename,"w");
                
        
            //    Check the file is opened without error
                if(Destinationfile == NULL)
                {
                    printf("file is not opened something problem in opening file");
                    return -1;        
                }    
                
            //    copy data from source file to destination file
                do                    
                {
                    
                    int ch = fgetc(Sourcefile);
                    if(!feof(Sourcefile))
                        fputc(ch,Destinationfile);
                                
                }    while(!feof(Sourcefile));                
                
                fclose(Sourcefile);
                fclose(Destinationfile);    
                
                remove(argv[1]);    
        }
        
        else
        {    
            printf("File has invalid extensions");
                        
        
        }
        
        return 0;
    }
    I would move the file to the subfolder by parsing file name to this program via command line argument.please help me to change my simple program to efficient one[that is code written by c professionals]...... [Sorry for my english] Thank you .....
    Last edited by hariharaan; 05-20-2015 at 02:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need ideas on how to improve my code
    By NumLock in forum C Programming
    Replies: 5
    Last Post: 08-10-2011, 01:53 PM
  2. Can I improve my code?
    By advancedk in forum C Programming
    Replies: 1
    Last Post: 07-27-2008, 07:47 AM
  3. Help me improve my code!
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-19-2006, 10:04 AM
  4. How to improve code
    By rugby in forum C Programming
    Replies: 3
    Last Post: 04-15-2003, 09:24 AM
  5. help improve my code
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 11:33 AM