Hi guys!
I tried whatever I could think of, it should be working, but I can't open the input file, I don't know why.
Code:#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <conio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
void Encrypt(char * FILENAME, char * NEW_FILENAME, char* process);
char* ExtensionRemover(char* mystr);
int is_regular_file(const char *path);
/* Global variables */
char AllStrings[256];
int main(void)
{
/* Local variables */
char process[50]; // array big enough to hold expected input
//char AfterProcess[50]="_";
DIR *d;
struct dirent *dir;
printf ("Please enter 'encryption or decryption'\n\n");
scanf ("%49s", process);
// Open the current directory
d = opendir(".");
if (d) // If the directory is not empty "if (d) != 0"
{
while ((dir = readdir(d)) != NULL) // While the directory is not empty
{
printf("\n%s\n", (dir->d_name)); // Print the name of the consequent file in the directory
// Add an if to check if the file is a directory like: is_regular_file(dir->d_name)
if (is_regular_file(dir->d_name) && strstr((dir->d_name), process) == 0) // We test a flag is it "0" or "1" to see if the name if of a file or directory
{
/* Always put brackets at (dir->d_name), (strcat(dir->d_name, process)) !!!!!!!!!!!!!!!!!!!!!!!!!!
Or you will get an error can't open inFile. */
Encrypt((dir->d_name), (strcat((dir->d_name), process)), process); // Encrypt(File1_Input, File2_Output, process)
}
}
closedir(d); // Close the directory
}
return(0); // Error code for everything is ok
}
// Encrypts or decrypts a file
void Encrypt(char * FILENAME, char * NEW_FILENAME, char* process)
{
printf("Processing started\n");
FILE *inFile;
FILE *outFile;
char key[256] = "RandomString"; // array big enough to hold expected input, dont give "char *" to scanf with "%s"
//char process[50]; // array big enough to hold expected input
int keylen, keyidx;
int Byte; // fgetc returns int, so use an int for "Byte" and "newByte"
int newByte;
// printf ("Please enter 'encryption or decryption'\n\n");
// scanf ("%49s", process);
// printf ("Please enter the key\n\n");
// scanf ("%49s", key);
keylen = strlen(key);
keyidx = 0; // starting index into key
printf("Opening files\n\n");
inFile = fopen(FILENAME,"rb");
outFile = fopen(NEW_FILENAME, "w");
if(inFile == NULL)
{
printf("Error: Can't Open inFile\n");
}
else if(outFile == NULL)
{
printf("Error: Can't open outFile\n");
}
else
{
printf("File Opened, processing.\n\n");
while((Byte = fgetc(inFile)) != EOF) // read a byte, check if EOF, "EOF" is outside the range of "char most times"
{
if (!strcmp(process,"encryption")) /* ??? "strmcp()" compares (process) a string to(encryption) a string */
{
printf (".");
newByte = Byte + key[keyidx]; // use key index
if (newByte > 256) newByte -= 256; // check for overflow
}
else if (!strcmp(process,"decryption"))
{
printf (".");
newByte = Byte - key[keyidx];
if (newByte < 0) newByte += 256;
}
else
{
newByte = Byte;
}
fputc(newByte, outFile);
keyidx++;
// loop to the start of the key if needed
if (keyidx >= keylen) keyidx = 0;
}
}
if (inFile != NULL) fclose(inFile); // close your files when you're done
if (outFile != NULL) fclose(outFile);
}
// Check if a file is a directory, the check must be update
// to include checks for other not used files, like pipes and etc.
int is_regular_file(const char *path)
{
struct stat path_stat;
stat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}