I am trying to make a simple text editor program in C, yet I have this weird bug. When I get to the first user prompt, the program crashes. Here is my code:
#include <stdio.h>
int main()
{
FILE *filenew;
char firstchoice[200];
char filenamenew[200];
char overwrite;
char *textwrite;
char *filenameopen;
FILE *fileopen;
char readchar;
char *textopen;
start:
puts("Welcome to the Texter Text Editor!");
printf("\n");
printf("\n");
puts("Type ~N~ to create a new document,");
puts("Type ~O~ to open an existing document,");
puts("And type ~Q~ to quit.");
scanf("%s",&firstchoice);
if(firstchoice=="~N~" || firstchoice=="~n~")
{
puts("Enter the filename of the new document:");
scanf("%s",&filenamenew);
filenew = fopen(filenamenew,"r");
if(filenew)
{
fclose(filenew);
printf("%s already exists!\nDo you wish to overwrite it? [Y/N]",filenamenew);
overwrite=getchar();
if(overwrite=='y' || overwrite=='Y')
{
filenew=fopen(filenamenew,"w");
goto textnew;
}
else if(overwrite=='N' || overwrite=='n')
{
goto start;
}
}
textnew:
if(!filenew)
{
do
{
scanf("%s",textwrite);
fprintf(filenew,"%s",textwrite);
}
while(textwrite!="~Q~" && textwrite!="~q~");
}
}
else if(firstchoice=="~q~" || firstchoice=="~Q~")
{
return(0);
}
else if (firstchoice=="~o~" || firstchoice=="~O~")
{
printf("Enter the filename of the document you want to open:\n");
scanf("%s",filenameopen);
fileopen=fopen(filenameopen,"r+");
if(!fileopen)
{
puts("File does not exist!");
goto start;
}
else
{
do
{
readchar=getc(fileopen);
putchar(readchar);
}
while(readchar!=EOF);
do
{
scanf("%s",textopen);
fprintf(fileopen,"%s",textopen);
}while(textopen!="~Q~" && textopen!="~q~");
}
}
return(0);
}
I know that it's messy, with all of the gotos and switching from char array to char pointer, but please try to help.