Thread: program create folders and transfer files into them

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    4

    Smile program create folders and transfer files into them

    I have 100 text files from A001 to A100, each file only contain a short string. I 'd like to know how to write a program to count the frequency of 'C', and create folders with filename_countvalue (like A100_12, A050_3, represent 12 and 3 of 'C' in A100 and A050), then sort file A100 to folder A100_12, etc. Could you tell me how to write the program? Thanks in advance.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    What have you tried?

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    Code:
    int main()
    {
    	FILE *in, *out;
    	int i,count;
    	char ch,str[3], filename[5];
    
    	for( i=1;i<100;i++)
    	       {......getfilename......
    		if((in=fopen(filename,"r"))==NULL)
    		continue;
    		count=0;
    		while(!feof(in))
    		  {
    			ch=fgetc(in);
    			if (ch=='c')
    			count+=1;
    		  }
                             sprintf(str,"%d",count);
                            mkdir(count,0777);
                           .........	         .......		
                            fclose(in);
    	        fclose(ou);
                    }
    }

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    after running, create a file named count, not filename_countvalue.
    How to create folder with variables? Thanks.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Creating directories and moving files is operating system dependent. Assuming you're using a *nix flavour, you can use mkdir from <sys/stat.h>
    mkdir

    To move a file/folder you can use the function rename in <stdio.h>:
    rename - C++ Reference

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    Quote Originally Posted by Memloop View Post
    Creating directories and moving files is operating system dependent. Assuming you're using a *nix flavour, you can use mkdir from <sys/stat.h>
    mkdir

    To move a file/folder you can use the function rename in <stdio.h>:
    rename - C++ Reference
    YES, I use linux fedora, why following program can't create folders 1-4:
    Code:
    int main()
     { int i;
        for (i=1,i<5,i++)
        mkdir(i,0777);
    }

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    First of all, I suggest you add this to your profile:
    Code:
    alias gcc='gcc -ansi -pedantic -Wall -Wextra'
    The code you've shown doesn't include all the needed headers, and if you did try to compile the code with the compiler warnings enabled you would get a lot of hints as to why it's not working.

    The first argument to mkdir is a char*, not an integer.

Popular pages Recent additions subscribe to a feed