Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<ctype.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<sys/dir.h>
- #include<dirent.h>
- #include<fcntl.h>
- void printInFile(char *file, char *buff)
- {
- int fis = open(file, O_WRONLY | O_APPEND);
- if(fis == -1)
- {
- printf("Error on printfile open\n");
- exit(1);
- }
- write(fis,buff,strlen(buff));
- close(fis);
- }
- void readFile(char* file, char *buff)
- {
- int fis = open(file, O_RDONLY);
- if(fis == -1)
- {
- perror("Error on readfile open!\n");
- exit(1);
- }
- int myRead;
- int nspaces=0, ndigits=0;
- while((myRead = read(fis, buff, 100)) > 0)
- {
- int i;
- for(i=0;i<myRead;i++)
- {
- if(buff[i] == ' ')
- nspaces++;
- if(isdigit(buff[i]))
- ndigits++;
- }
- }
- char str[100];
- sprintf(str," has %d spaces, %d digits.\n",nspaces,ndigits);
- printInFile("output.txt",str);
- close(fis);
- }
- void getDataFromDir(char *dirName)
- {
- char buff[100];
- char Path[100];
- DIR *dir;
- struct stat fileStat;
- struct dirent *entry;
- dir = opendir(dirName);
- if(dir==NULL)
- {
- perror("opendir");
- exit(1);
- }
- while( (entry = readdir(dir)) !=NULL)
- {
- //printf("%s\n",entry->d_name);
- if( (strcmp(entry->d_name,".") == 0) || (strcmp(entry->d_name,"..") ==0) )
- continue;
- sprintf(Path,"%s/%s",dirName, entry->d_name);
- if((stat(Path,&fileStat)) == 0)//ok
- {
- printf("Calea fis este: %s\n",Path);
- if(S_ISREG(fileStat.st_mode))
- {
- //printf("Fisierul: %s\n\n",entry->d_name);
- sprintf(buff, "%s ", Path);
- printInFile("output.txt", buff);
- readFile(Path, buff);
- }
- if(S_ISDIR(fileStat.st_mode))
- {
- getDataFromDir(Path);
- }
- }
- }
- }
- int main(int argc, char *argv[])
- {
- if(argc < 2)
- {
- printf("Error, correct usage: directory_name output_file_name\n");
- exit(1);
- }
- getDataFromDir(argv[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement