Ok from what I have read it sounds like its easier to open and read the file in the one function and then go to another function to decode the file.
This is now the openFile function
Code:
void OpenFile(void)
{
FILE *fp;
long lSize;
char * buffer;
size_t result;
char fname[100]="";
printf("\n\tEnter File Name: ");
scanf("%s",&fname);
fp=fopen(fname,"rb");
if(fp==NULL)
{
printf("\n%s\" File Not Found!",fname);
getchar();
}
else
{
fseek (fp , 0 , SEEK_END);
lSize = ftell (fp);
rewind (fp);
buffer = (char*) malloc (sizeof(char)*lSize);
result = fread (buffer,1,lSize,fp);
fclose (fp);
free (buffer);
load_menu2();
}
}
Would that be correct? So the file is now loaded into an array? So then when I call the decode function after I choose option 1 in the load_menu2, that function will have to split each 8 bit number in the array into 2 4 bit numbers and then I point the value of each 4 bit number to my array decodingTable[]?
Full program again.
Code:
#include <stdio.h>
#include <stdlib.h>
unsigned char decodingTable[] = { 0, 1, 3, 2, 7, 6, 4, 5, 0xF, 0xE, 0xC, 0xD, 8, 9, 0xB, 0xA };
void load_menu2(void);
void OpenFile(void);
unsigned char decode();
int main()
{
int choice;
do
{
printf("\n\n\t\tMenu:\n");
printf("\n\n\t\t[1] Open a File (Supported .grc)\n");
printf("\t\t[2] Exit\n");
printf("\n\n\t\tPlease Select and Option [1 or 2]: ");
scanf("%d", &choice);
switch(choice)
{
case 1:OpenFile();
break;
case 2: printf("\nProgram Terminated!\n");
exit(0);
break;
default: printf("\nInvalid Choice!\n");
break;
}
}
while (choice !=2);
return 0;
}
void OpenFile(void)
{
FILE *fp;
long lSize;
char * buffer;
size_t result;
char fname[100]="";
printf("\n\tEnter File Name: ");
scanf("%s",&fname);
fp=fopen(fname,"rb");
if(fp==NULL)
{
printf("\n%s\" File Not Found!",fname);
getchar();
}
else
{
fseek (fp , 0 , SEEK_END);
lSize = ftell (fp);
rewind (fp);
buffer = (char*) malloc (sizeof(char)*lSize);
result = fread (buffer,1,lSize,fp);
fclose (fp);
free (buffer);
load_menu2();
}
}
void load_menu2()
{
int choice2;
do
{
printf("\n\n\t\tMenu:\n");
printf("\n\n\t\t[1] Decode the Sequence\n");
printf("\t\t[2] Exit\n");
printf("\n\n\t\tPlease Select and Option [1 or 2]: ");
scanf("%d", &choice2);
switch(choice2)
{
case 1: decode();
break;
case 2: printf("\nProgram Terminated!\n");
exit(0);
break;
default: printf("\nInvalid Choice!\n");
break;
}
} while (choice2 !=2);
}
unsigned char decode()
{
printf("works");
}