Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <dirent.h>
- #define PATH "./labyrinth"
- void writePath(char* startPath, char* name)
- {
- strcat(strcat(startPath, "/"), name);
- }
- void clearFile()
- {
- FILE *file = fopen("./result.txt", "w");
- fclose(file);
- }
- void writeInFile(char* output)
- {
- FILE *file = fopen("./result.txt", "r");
- char buf[10000] = "\0";
- char miniBuf[500] = "\0";
- while (fgets(miniBuf, 500, file) != NULL)
- {
- strcat(buf,miniBuf);
- }
- fclose(file);
- file = fopen("./result.txt", "w");
- fprintf(file, "%s\n", output);
- fprintf(file, "%s", buf);
- fclose(file);
- }
- int readFile(char* path);
- int searchInDir(char* path, char* filename);
- int main()
- {
- char path[1000] = PATH;
- char filename[100] = "file.txt";
- clearFile();
- searchInDir(path, filename);
- return 0;
- }
- int readFile(char* path)
- {
- char buf[100];
- FILE *file = fopen(path, "r");
- while (fgets(buf, 100, file) != NULL)
- {
- buf[strlen(buf)-1] = '\0';
- if(!strcmp(buf, "Deadlock"))
- {
- //return 0;
- }
- else if(!strcmp(buf, "Minotaur"))
- {
- return 1;
- }
- else{
- memcpy(buf, buf+9, 91);
- char newPath[1000] = PATH;
- if (searchInDir(newPath, buf))
- return 1;
- }
- }
- fclose(file);
- return 0;
- }
- int searchInDir(char* path, char* filename)
- {
- char buf[1000];
- strcpy(buf, path);
- DIR* dir = opendir(path);
- if (dir){
- struct dirent *point = readdir(dir);
- while (point){
- if ((point->d_type == DT_DIR) && (strcmp(point->d_name, ".")) && (strcmp(point->d_name, "..")))
- {
- writePath(path, point->d_name);
- if (searchInDir(path, filename))
- {
- closedir(dir);
- return 1;
- }
- else
- strcpy(path, buf);
- }
- else if (!strcmp(point->d_name, filename))
- {
- closedir(dir);
- char output[1000];
- writePath(path, point->d_name);
- strcpy(output, path);
- if (readFile(path))
- {
- writeInFile(output);
- return 1;
- }
- else
- return 0;
- }
- point = readdir(dir);
- }
- }
- closedir(dir);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement