[英]Bug in Text Editor Program
我试图用C语言编写一个简单的文本编辑器程序,但是我有这个奇怪的错误。 当我到达第一个用户提示符时,程序崩溃。 这是我的代码:
#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);
}
我知道这很麻烦,所有的getos都从char数组切换到char指针,但是请尝试提供帮助。
我可以看到的第一个问题是字符串比较:
firstchoice=="~N~"
应该
strcmp(firstchoice, "~N~") == 0
您比较了指针的值而不是字符串的值,因此所有比较都失败了,程序只返回到return子句。
关于以下分段错误:
您打开一个新文件, filenew = fopen(filenamenew,"r");
,如果文件不存在( if(!filenew)
),则尝试写入该文件( fprintf(filenew,"%s",textwrite);
)。 您需要先打开它才能编写。
您调用scanf("%s",textwrite);
当textwrite
是未初始化的指针并且没有指向缓冲区时,它应该是数组或指向已分配内存的指针(例如,通过malloc)。 您的代码中的以下变量存在此错误:
通过之后,我认为大多数问题都将被抛在后面。
strcmp
进行比较。 还有stricmp
,它不区分大小写。 &
。 char *textwrite;
...
scanf("%s",textwrite);
您从未为textwrite
分配内存。 你应该尝试像
textwrite = malloc(sizeof(char) * 128);
scanf("127%s" , textwrite);
我不知道这是否真的是您的问题(尚未)。
要停止崩溃,请不要传入firstchoice
的指针,而firstchoice
传入变量本身。
scanf("%s",firstchoice);
对于数组,不需要为scanf
类的函数获取指针。 您也可以使用其他scanf
执行此操作。
代码中有几个问题,我将不做详细介绍,因为显然这是您的学习练习。 但是,第一个,摆脱goto
。
scanf
需要指向char数组的指针,您正在将其传递给指向char数组的指针的指针
scanf("%s",&firstchoice);
以上行需要
scanf("%s",firstchoice);
要么
scanf("%s",&firstchoice[0]);
另外,不能简单地使用==
将char数组与字符串文字进行比较,而必须使用strcmp
。
编译代码会产生以下警告。 我建议您开始注意它们。 如果看不到它们,请提高编译器的警告级别。
prog.c: In function ‘main’:
prog.c:22: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[200]’
prog.c:23: warning: comparison with string literal results in unspecified behavior
prog.c:23: warning: comparison with string literal results in unspecified behavior
prog.c:26: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[200]’
prog.c:53: warning: comparison with string literal results in unspecified behavior
prog.c:53: warning: comparison with string literal results in unspecified behavior
prog.c:57: warning: comparison with string literal results in unspecified behavior
prog.c:57: warning: comparison with string literal results in unspecified behavior
prog.c:61: warning: comparison with string literal results in unspecified behavior
prog.c:61: warning: comparison with string literal results in unspecified behavior
prog.c:83: warning: comparison with string literal results in unspecified behavior
prog.c:83: warning: comparison with string literal results in unspecified behavior
prog.c:22: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:26: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:49: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:64: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:81: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:49: warning: ‘textwrite’ may be used uninitialized in this function
prog.c:64: warning: ‘filenameopen’ may be used uninitialized in this function
prog.c:81: warning: ‘textopen’ may be used uninitialized in this function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.