Still confused and sad about the loop not being exact.
Also if I use the same code in the program this manner, when it decodes the program crashes. I'm clueless. Help please.
Code:
#include<stdio.h>
///GLOBALS
FILE *fptr;
char path[80];
char msg[100];
char choice;
int n=0;
/////////////
void thedoor()
{
printf("Incorrect choice.");
}
void encode()
{
printf("Write something:\n");
gets(msg);
printf("Encoded without failure!\n");
while(msg[n]!='\0')
{
printf("%d ",msg[n]);
n++;
}
printf("\nWrite this to file?:");
choice=getchar();
getchar();
if(choice=='Y'||choice=='y')
{
printf("Enter filename (full path maybe):");
gets(path);
n=0;
fptr = fopen(path,"w");
if(fptr==NULL)
{
printf("Error creating the file.");
}
while(msg[n]!='\0')
{
fprintf(fptr,"%d ",msg[n]);
n++;
}
fprintf(fptr,"%c",'\0');
fclose(fptr);
}
else
printf("See ya...");
}
void decode()
{
printf("\nEnter a file name to decipher(path if required):");
gets(path);
fptr= fopen(path,"r");
if(fptr==NULL)
{
printf("Error opening the file.");
}
else
{
fscanf(fptr,"%d",&msg[0]);
printf("\nThis is what has been said:\n");
while(msg[n]!=EOF)
{
fscanf(fptr,"%d",&msg[n]);
n++;
}
puts(msg);
}
}
int main()
{
printf("Encode or Decode?(E\\D):");
choice=getchar();
getchar();
if(choice=='E'||choice=='e')
encode();
else if(choice=='D'||choice=='d')
decode();
else
thedoor();
return 0;
}