Ok first question regarding to pointers (assuming the code is being run on windows and not linux):
we can do this :
int i = 10;
int *p1 = &i;
int *p2 = p2;
int *p3 = p3;
so we create an int with the value of 10
and 3 pointers the first points to the address of i, the second points to the first pointer which points to address of i, and the third points to the address of the second pointer which points to the address of the first pointer which points to the address of i.
So they all pointing at the same address which holds the value 10.
Now if I were to do this :
char ch[] = "Hello";
char *p1 = ch;
char *p2 = p1;
char *p3 = p2;
printf("ch : %s\n", ch);
printf("p1 address [%d] value is %s\n", p1, *p1);
printf("p2 address [%d] value is %s\n", p2, *p2);
printf("p3 address [%d] value is %s\n", p3, *p3);
I'll get error after printing ch and I don't understand why,
I though that if I write char *p1 = ch; that means p1 points to the address of the first character in the array and the other pointers point to pointer 1 eventually which points to the address of the first element in the array BUT the program crashes and I don't understand why ..
Another question is when should I use characters array and when should I use pointers ? From what I've read, and please correct me if I'm wrong, character pointers are not modify-able unless you allocate memory to it using malloc() and then you can read AND write to the different addresses but if you were to do something like
char *p = "str";
this is not writeable and it's constant, and about what happens if you do something like
char *p1 = "str";
char *p2 = p1;
I'm not sure if it's writeable, heck, I'm not even sure if I can do it since I tried to do it in my example shown in question #1 and the program crashes.
My last question is this:
I decided to make a File Ceasar Shift program, I enter the shift and the file to encrypt through the arguments in the cmd, I've created a file named CryptoFile.txt which holds some text in it, When I run the program this is what I've executed in the cmd :
"FileCaesarShift.exe 15 CryptoFile.txt"
so the shift is 15 and the file to encrypt is CryptoFile.txt
Now.. although a new file called CryptoFile.enc is being crated it's EMPTY and I don't see why, I've tried to look for the problem but failed.
Here's the code :
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void copyFileExtension(char*, char*);
int getFileNameLengthWithoutExtension(char*);
int getFileExtensionLength(char*);
void copyFileNameWithoutExtension(char*, char*);
int main(int argc, char *argv[])
{
int shift = atoi(argv[1]);
int byte;
char *fileName = (char*)argv[2];
char *fileExtension = malloc(getFileExtensionLength(fileName) + 1);
copyFileExtension(fileExtension, fileName);
char *newFileName = malloc(getFileNameLengthWithoutExtension(fileName) + 5);
copyFileNameWithoutExtension(newFileName, fileName);
FILE *f_in;
FILE *f_out;
f_in = fopen(fileName, "r");
if (strcmp(fileExtension, "enc") == 0)
{
// We want to decrypt the file
puts("dec");
strcat(newFileName, ".dec");
f_out = fopen(newFileName, "w");
while ((byte = fgetc(f_in)) != EOF)
{
fputc(byte - shift, f_out);
}
}
else
{
puts("enc");
// We want to encrypt the file
strcat(newFileName, ".enc");
f_out = fopen(newFileName, "w");
while ((byte = fgetc(f_in)) != EOF)
{
fputc(byte + shift, f_out);
}
}
fclose(f_in);
fclose(f_out);
return 0;
}
void copyFileExtension(char *fileExtension, char *fileName)
{
char *token = strtok(fileName, ".");
token = strtok(NULL, ".");
strcpy(fileExtension, token);
}
int getFileNameLengthWithoutExtension(char *fileName)
{
if (*fileName && *fileName != '.')
{
return 1 + getFileNameLengthWithoutExtension(++fileName);
}
return 0;
}
int getFileExtensionLength(char *fileName)
{
int foundExt = 0;
int len = 0;
while(*fileName)
{
if (foundExt == 1)
{
len++;
}
if (*fileName == '.')
{
foundExt = 1;
}
fileName++;
}
printf("ext len is %d\n", len);
return len;
}
void copyFileNameWithoutExtension(char* dest, char *source)
{
char *fileNameWithoutExtension = strtok(source, ".");
strcpy(dest, fileNameWithoutExtension);
}
I guess it's a problem with the while statement
"while ((byte = fgetc(f_in)) != EOF)"
because I get inside the else statement and a proof of it is the printf to the console saying "enc" + a new file being created which is enough proof that I get inside the else statement BUT as I said the file is empty so it doesn't go through the while loop but I don't see what's wrong with it, to me it looks fine :|