C language Help using Dirent.h
-
well.. im trying to open a folder with an unknown number of programs using argv and argc through the cmd to the adress of the folder i need to use the dirent.h and maybe the FILE struct to open the files the code needs to open each file and search the text within (it has mp3 signatures etc) in a binary way and search through it with a second file that the text inside of it is not in any place in there in other words i need to spot if the second file's signature is nto repeatign anywhere on the other files this is the far that i got but im stuck can anyone help me?
#include <stdio.h>
#include <stdlib.h>
#include "dirent.h"int main(int argc, char** argv){
int count = 0, size = 0; int sizes\[3\]; FILE \*log = fopen("C:\\\\Users\\\\win7\\\\Desktop\\\\1\\\\C2\_Mid\_Anti-Virus\_Project.zip\\\\AntiVirusLog.txt", "wt"); DIR \*dir; struct dirent \*ent; int found = 0; /\* Open directory \*/ dir = opendir(argv\[1\]); while ((ent = readdir(dir))) { count++; sizes\[0\] = strlen(argv\[1\]); sizes\[1\] = 2; sizes\[2\] = strlen(ent->d\_name); size += sizes\[0\] + sizes\[1\] + sizes\[2\] + 1; char\* string = (char\*)malloc(sizeof(char)\*size); \_flushall(); strcpy(string,argv\[1\]); strcat(string, "\\\\"); strcat(string, ent->d\_name); FILE\* youtube = fopen (string, "rb"); if (youtube != NULL) { print("success \\n"); } puts(ent->d\_name); } fclose(log); closedir(dir); return 0;
}
-
well.. im trying to open a folder with an unknown number of programs using argv and argc through the cmd to the adress of the folder i need to use the dirent.h and maybe the FILE struct to open the files the code needs to open each file and search the text within (it has mp3 signatures etc) in a binary way and search through it with a second file that the text inside of it is not in any place in there in other words i need to spot if the second file's signature is nto repeatign anywhere on the other files this is the far that i got but im stuck can anyone help me?
#include <stdio.h>
#include <stdlib.h>
#include "dirent.h"int main(int argc, char** argv){
int count = 0, size = 0; int sizes\[3\]; FILE \*log = fopen("C:\\\\Users\\\\win7\\\\Desktop\\\\1\\\\C2\_Mid\_Anti-Virus\_Project.zip\\\\AntiVirusLog.txt", "wt"); DIR \*dir; struct dirent \*ent; int found = 0; /\* Open directory \*/ dir = opendir(argv\[1\]); while ((ent = readdir(dir))) { count++; sizes\[0\] = strlen(argv\[1\]); sizes\[1\] = 2; sizes\[2\] = strlen(ent->d\_name); size += sizes\[0\] + sizes\[1\] + sizes\[2\] + 1; char\* string = (char\*)malloc(sizeof(char)\*size); \_flushall(); strcpy(string,argv\[1\]); strcat(string, "\\\\"); strcat(string, ent->d\_name); FILE\* youtube = fopen (string, "rb"); if (youtube != NULL) { print("success \\n"); } puts(ent->d\_name); } fclose(log); closedir(dir); return 0;
}
-
well.. im trying to open a folder with an unknown number of programs using argv and argc through the cmd to the adress of the folder i need to use the dirent.h and maybe the FILE struct to open the files the code needs to open each file and search the text within (it has mp3 signatures etc) in a binary way and search through it with a second file that the text inside of it is not in any place in there in other words i need to spot if the second file's signature is nto repeatign anywhere on the other files this is the far that i got but im stuck can anyone help me?
#include <stdio.h>
#include <stdlib.h>
#include "dirent.h"int main(int argc, char** argv){
int count = 0, size = 0; int sizes\[3\]; FILE \*log = fopen("C:\\\\Users\\\\win7\\\\Desktop\\\\1\\\\C2\_Mid\_Anti-Virus\_Project.zip\\\\AntiVirusLog.txt", "wt"); DIR \*dir; struct dirent \*ent; int found = 0; /\* Open directory \*/ dir = opendir(argv\[1\]); while ((ent = readdir(dir))) { count++; sizes\[0\] = strlen(argv\[1\]); sizes\[1\] = 2; sizes\[2\] = strlen(ent->d\_name); size += sizes\[0\] + sizes\[1\] + sizes\[2\] + 1; char\* string = (char\*)malloc(sizeof(char)\*size); \_flushall(); strcpy(string,argv\[1\]); strcat(string, "\\\\"); strcat(string, ent->d\_name); FILE\* youtube = fopen (string, "rb"); if (youtube != NULL) { print("success \\n"); } puts(ent->d\_name); } fclose(log); closedir(dir); return 0;
}
Looks to me like you're most of the way there. You probably want to read in your reference file into a buffer. You can use _stat() to get the file size for malloc(). Next, it depends on where the signatures are in the file. If they're at a known location you could use fseek(), fread() and memcmp() to determine if the signature is in the file or not. If the signatures are at a random location, I'd pull the entire file into a malloced buffer (_stat() again), and then search through the buffer. The simple way would be use memcmp() at locations 0 ... (current_file_size - reference_file_size), but there's more efficent ways of going about that. If you know that there's no null chars in the either file (unlikely), maybe strstr() is an option. some notes on what you have so far: sizes[0], and sizes[1] will not change over the while loop, so they could be computed before entering the loop. strlen("\\") is 1, not 2 you not calling free(string) within the while loop, so you're leaking memory. If you use my suggestion about slurping the file into memory, don't forget to free() that buffer too.
-
Looks to me like you're most of the way there. You probably want to read in your reference file into a buffer. You can use _stat() to get the file size for malloc(). Next, it depends on where the signatures are in the file. If they're at a known location you could use fseek(), fread() and memcmp() to determine if the signature is in the file or not. If the signatures are at a random location, I'd pull the entire file into a malloced buffer (_stat() again), and then search through the buffer. The simple way would be use memcmp() at locations 0 ... (current_file_size - reference_file_size), but there's more efficent ways of going about that. If you know that there's no null chars in the either file (unlikely), maybe strstr() is an option. some notes on what you have so far: sizes[0], and sizes[1] will not change over the while loop, so they could be computed before entering the loop. strlen("\\") is 1, not 2 you not calling free(string) within the while loop, so you're leaking memory. If you use my suggestion about slurping the file into memory, don't forget to free() that buffer too.
Ok i somehow managed out to open the files BUT all I have left now is to search inside these files for the signature
-
Ok i somehow managed out to open the files BUT all I have left now is to search inside these files for the signature
First you need to write down the steps you require to do the searching. Something like:
Read 'key' file and extract its signature
For each file in directory
Do
Open next file
Read some data (maybe a small amount, maybe all, or maybe just some specific block)
If the key-file signature is in the data of this file
Then
Do file match processing
Else
Do file no match processing (if necessary)
EndIf
Until (no more files)Once you have all the steps clearly defined it should not be too difficult to turn that into code.
-
First you need to write down the steps you require to do the searching. Something like:
Read 'key' file and extract its signature
For each file in directory
Do
Open next file
Read some data (maybe a small amount, maybe all, or maybe just some specific block)
If the key-file signature is in the data of this file
Then
Do file match processing
Else
Do file no match processing (if necessary)
EndIf
Until (no more files)Once you have all the steps clearly defined it should not be too difficult to turn that into code.
thank you but atm im trying to figure out how to open the folder that contains the folder that I entered in argv because its a fodler inside a folder that contains files as you can see it doesnt work int count = 0, size = 0, totalDirs = 0,location = 0; int sizes[3],flag = 0; int secSpot = 0; int argvSize, spot = 0; char c; argvSize = strlen(argv[1]); for (int i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { spot++; } } for (int i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { if (secSpot != spot) { secSpot++; } else if ((secSpot == spot) && (flag == 0)) { location = i; flag++; } } } location--; char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*location); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } puts(entrenceToTheFolderBefore); free(entrenceToTheFolderBefore);
-
thank you but atm im trying to figure out how to open the folder that contains the folder that I entered in argv because its a fodler inside a folder that contains files as you can see it doesnt work int count = 0, size = 0, totalDirs = 0,location = 0; int sizes[3],flag = 0; int secSpot = 0; int argvSize, spot = 0; char c; argvSize = strlen(argv[1]); for (int i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { spot++; } } for (int i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { if (secSpot != spot) { secSpot++; } else if ((secSpot == spot) && (flag == 0)) { location = i; flag++; } } } location--; char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*location); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } puts(entrenceToTheFolderBefore); free(entrenceToTheFolderBefore);
A folder in a folder is just a new path to search. When you get a directory in your dirent search, you just append a backslash
'\\'
followed by the directory name to the string you got from argv (or the current path), and start a new search. Say you start your program by:program C:\Users\Random\Lists
and your found entry is a directory named Music, you would create a new path C:\Users\Random\Lists\Music and continue with that. Don't forget to ignore directories named
.
and..
as they lead back up the tree. -
A folder in a folder is just a new path to search. When you get a directory in your dirent search, you just append a backslash
'\\'
followed by the directory name to the string you got from argv (or the current path), and start a new search. Say you start your program by:program C:\Users\Random\Lists
and your found entry is a directory named Music, you would create a new path C:\Users\Random\Lists\Music and continue with that. Don't forget to ignore directories named
.
and..
as they lead back up the tree.insteadof entering "\\" my teacher told me that I could use / to make it quicker my problem is tho that when I do puts i get garbage and not the output i wanted as a link outside of the argv folder
-
insteadof entering "\\" my teacher told me that I could use / to make it quicker my problem is tho that when I do puts i get garbage and not the output i wanted as a link outside of the argv folder
it seems that the problem is within these lines for (int i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { if (secSpot != spot) { secSpot++; } if (secSpot == spot) { if (flag == 0) { location = i; flag++; } } } } _flushall(); char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*location); int a; a = strlen(entrenceToTheFolderBefore); printf("%d", a); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } when the entrenceToTheFolderBefore has the size of 40 bits and not 23 like it was suppsoe to the argv that im using is C:/Users/win7/Desktop/1/C2_Mid_Anti - Virus_Project.zip
-
it seems that the problem is within these lines for (int i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { if (secSpot != spot) { secSpot++; } if (secSpot == spot) { if (flag == 0) { location = i; flag++; } } } } _flushall(); char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*location); int a; a = strlen(entrenceToTheFolderBefore); printf("%d", a); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } when the entrenceToTheFolderBefore has the size of 40 bits and not 23 like it was suppsoe to the argv that im using is C:/Users/win7/Desktop/1/C2_Mid_Anti - Virus_Project.zip
Sorry, but that code does not make any sense. I have no idea what the first block of code is supposed to be doing. And in the code following the
flushall
call, you have made a call tostrlen
on a pointer returned frommalloc
. But the memory that it points to has not been initialised with a string, so the value you get will be either zero, or some random invalid number. You must usestrlen
on the source string (argv[1]
) in order to measure it. So your code should be something like:int length = strlen(argv[1]) + 1; // extra space for trailing null character
char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char) * length);
for (int i = 0; i < length; i++)
{
entrenceToTheFolderBefore[i] = argv[1][i];
}You could use
strcpy
here, but perhaps your teacher has told you not to. -
Sorry, but that code does not make any sense. I have no idea what the first block of code is supposed to be doing. And in the code following the
flushall
call, you have made a call tostrlen
on a pointer returned frommalloc
. But the memory that it points to has not been initialised with a string, so the value you get will be either zero, or some random invalid number. You must usestrlen
on the source string (argv[1]
) in order to measure it. So your code should be something like:int length = strlen(argv[1]) + 1; // extra space for trailing null character
char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char) * length);
for (int i = 0; i < length; i++)
{
entrenceToTheFolderBefore[i] = argv[1][i];
}You could use
strcpy
here, but perhaps your teacher has told you not to.I dont know if i fixed it or not because Just now i saw your comment but this is how far i got in order to get to the second frole from the first file but for some odd reason it stopps when i do malloc #include #include #include "dirent.h" int main(int argc, char** argv) { DIR* OtherDir; struct dirent *OtherEnt; int location = 0, flag = 0, secSpot = 0, argvSize, spot = 0,i; argvSize = strlen(argv[1]); for (i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { spot++; } } puts(argv[1]); for (i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { if (secSpot != spot) { secSpot++; } if (secSpot == spot) { if (flag == 0) { location = i; flag++; } } } } char* UnwantedName = (char*)malloc(NULL); //<--- problem for (int i = location + 1; i < argvSize; i++) { strcat(UnwantedName,"/C2_Mid_Anti-Virus_Project.zip"); } _flushall(); char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*location); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } _flushall(); OtherDir = opendir(entrenceToTheFolderBefore); char* notPath1 = (char*)malloc(NULL); char* notPath2 = (char*)malloc(NULL); strcpy(notPath1, argv[1]); strcpy(notPath2, argv[1]); strcat(notPath1, "/."); strcat(notPath2, "/.."); char* InfectedFolderPath = (char*)malloc(NULL); while (OtherEnt = readdir(OtherDir)) { if ((strcmp(OtherEnt->d_name, UnwantedName) != 0 && (strcmp(UnwantedName, notPath1) != 0) && (strcmp(UnwantedName, notPath2) != 0))) { strcpy(InfectedFolderPath, entrenceToTheFolderBefore); strcat(InfectedFolderPath, "/"); strcat(InfectedFolderPath, OtherEnt->d_name); puts(InfectedFolderPath); } } free(UnwantedName); free(entrenceToTheFolderBefore); free(InfectedFolderPath); free(notPath1); free(notPath2); }
-
I dont know if i fixed it or not because Just now i saw your comment but this is how far i got in order to get to the second frole from the first file but for some odd reason it stopps when i do malloc #include #include #include "dirent.h" int main(int argc, char** argv) { DIR* OtherDir; struct dirent *OtherEnt; int location = 0, flag = 0, secSpot = 0, argvSize, spot = 0,i; argvSize = strlen(argv[1]); for (i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { spot++; } } puts(argv[1]); for (i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { if (secSpot != spot) { secSpot++; } if (secSpot == spot) { if (flag == 0) { location = i; flag++; } } } } char* UnwantedName = (char*)malloc(NULL); //<--- problem for (int i = location + 1; i < argvSize; i++) { strcat(UnwantedName,"/C2_Mid_Anti-Virus_Project.zip"); } _flushall(); char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*location); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } _flushall(); OtherDir = opendir(entrenceToTheFolderBefore); char* notPath1 = (char*)malloc(NULL); char* notPath2 = (char*)malloc(NULL); strcpy(notPath1, argv[1]); strcpy(notPath2, argv[1]); strcat(notPath1, "/."); strcat(notPath2, "/.."); char* InfectedFolderPath = (char*)malloc(NULL); while (OtherEnt = readdir(OtherDir)) { if ((strcmp(OtherEnt->d_name, UnwantedName) != 0 && (strcmp(UnwantedName, notPath1) != 0) && (strcmp(UnwantedName, notPath2) != 0))) { strcpy(InfectedFolderPath, entrenceToTheFolderBefore); strcat(InfectedFolderPath, "/"); strcat(InfectedFolderPath, OtherEnt->d_name); puts(InfectedFolderPath); } } free(UnwantedName); free(entrenceToTheFolderBefore); free(InfectedFolderPath); free(notPath1); free(notPath2); }
It would help if you formatted your code properly with <pre> tags around it, and removed the extra blank lines. The following is wrong, you cannot malloc nothing.
char* UnwantedName = (char*)malloc(NULL); //<--- problem
You must provide the size, in bytes, that you want to allocate, like
char* UnwantedName = (char*)malloc(argvSize);
-
It would help if you formatted your code properly with <pre> tags around it, and removed the extra blank lines. The following is wrong, you cannot malloc nothing.
char* UnwantedName = (char*)malloc(NULL); //<--- problem
You must provide the size, in bytes, that you want to allocate, like
char* UnwantedName = (char*)malloc(argvSize);
I tried editing it to char* entrenceToTheFolderBefore =(char*)malloc(sizeof(char)*location); but it still triggers a break point
-
I tried editing it to char* entrenceToTheFolderBefore =(char*)malloc(sizeof(char)*location); but it still triggers a break point
this is odd it seems that after switching place wit hthe malloc above it was ok
-
It would help if you formatted your code properly with <pre> tags around it, and removed the extra blank lines. The following is wrong, you cannot malloc nothing.
char* UnwantedName = (char*)malloc(NULL); //<--- problem
You must provide the size, in bytes, that you want to allocate, like
char* UnwantedName = (char*)malloc(argvSize);
Now that I fixed most of the code the last part is which is the loop to find out the name of the second folder Reminder that The argv has a path to a folder the program is trying to exit the folder to the fodler before (succeeded) and now at the last part trying to get the path for the second folder and the file before if it makes any sence so for some odd reason it doesnt work the weirder part is when i try to do puts(); on the Dir struct d_name and it triggers break point
//UnwatedName = /C2_Mid_Anti-Virus_Project.zip notPath1 = '.' notPath2=".."
while (OtherEnt = readdir(OtherDir))
{puts(OtherEnt->d\_name); if ((OtherEnt->d\_name != UnwantedName) && (OtherEnt->d\_name != notPath1) && (OtherEnt->d\_name != notPath2)) { puts(OtherEnt->d\_name); strcpy(InfectedFolderPath, entrenceToTheFolderBefore); strcat(InfectedFolderPath, "/"); strcat(InfectedFolderPath, OtherEnt->d\_name); puts(InfectedFolderPath); }
}
-
this is odd it seems that after switching place wit hthe malloc above it was ok
Go to ParentNow that I fixed most of the code the last part is which is the loop to find out the name of the second folder Reminder that The argv has a path to a folder the program is trying to exit the folder to the fodler before (succeeded) and now at the last part trying to get the path for the second folder and the file before if it makes any sence so for some odd reason it doesnt work the weirder part is when i try to do puts(); on the Dir struct d_name and it triggers break point Hide Copy Code //UnwatedName = /C2_Mid_Anti-Virus_Project.zip notPath1 = '.' notPath2=".." while (OtherEnt = readdir(OtherDir)) { puts(OtherEnt->d_name); if ((OtherEnt->d_name != UnwantedName) && (OtherEnt->d_name != notPath1) && (OtherEnt->d_name != notPath2)) { puts(OtherEnt->d_name); strcpy(InfectedFolderPath, entrenceToTheFolderBefore); strcat(InfectedFolderPath, "/"); strcat(InfectedFolderPath, OtherEnt->d_name); puts(InfectedFolderPath); } }
-
Go to ParentNow that I fixed most of the code the last part is which is the loop to find out the name of the second folder Reminder that The argv has a path to a folder the program is trying to exit the folder to the fodler before (succeeded) and now at the last part trying to get the path for the second folder and the file before if it makes any sence so for some odd reason it doesnt work the weirder part is when i try to do puts(); on the Dir struct d_name and it triggers break point Hide Copy Code //UnwatedName = /C2_Mid_Anti-Virus_Project.zip notPath1 = '.' notPath2=".." while (OtherEnt = readdir(OtherDir)) { puts(OtherEnt->d_name); if ((OtherEnt->d_name != UnwantedName) && (OtherEnt->d_name != notPath1) && (OtherEnt->d_name != notPath2)) { puts(OtherEnt->d_name); strcpy(InfectedFolderPath, entrenceToTheFolderBefore); strcat(InfectedFolderPath, "/"); strcat(InfectedFolderPath, OtherEnt->d_name); puts(InfectedFolderPath); } }
You cannot use
!=
to compare strings, you need to usestrcmp
. If you are going to append a new directory name to an existing path then you need to allocate the sum of, the length of each string, plus 1 for the extra backslash, plus 1 for the trailing null. I would strongly suggest you get some decent learning materials and study the basics of the C language and standard libraries, before continuing with this somewhat advanced project. -
You cannot use
!=
to compare strings, you need to usestrcmp
. If you are going to append a new directory name to an existing path then you need to allocate the sum of, the length of each string, plus 1 for the extra backslash, plus 1 for the trailing null. I would strongly suggest you get some decent learning materials and study the basics of the C language and standard libraries, before continuing with this somewhat advanced project.to be honest the first thing i used was strcmp i tried to do so many combinatiosn and in so many different ways that im out of ideas so i tried anything that could might help
-
to be honest the first thing i used was strcmp i tried to do so many combinatiosn and in so many different ways that im out of ideas so i tried anything that could might help
That is what I guessed from looking at all your posts. And that is why I suggested you stop trying random pieces of code, and go and work through some tutorials and reference guides on the basics of C and its run-time libraries. Trying to learn programming from posting questions here is really not a good idea. A few, or many, hours of serious study will serve you much better in the long term.
-
That is what I guessed from looking at all your posts. And that is why I suggested you stop trying random pieces of code, and go and work through some tutorials and reference guides on the basics of C and its run-time libraries. Trying to learn programming from posting questions here is really not a good idea. A few, or many, hours of serious study will serve you much better in the long term.
you have been very helpfull to me and believe it or not im so close to finish the first exercise of the project as you adviced I will study the materials again but it will be after I will hand it out because I have left a few more hours to work on it till it hits 11:50 pm