我正在尝试用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指针,但请尽量帮助。
答案 0 :(得分:4)
我能看到的第一个问题是字符串比较:
firstchoice=="~N~"
应该是
strcmp(firstchoice, "~N~") == 0
你比较了指针的值而不是字符串,所以所有比较都失败了,程序只得到了return子句。
关于以下之后的分段错误:
您打开一个新文件filenew = fopen(filenamenew,"r");
,如果该文件不存在(if(!filenew)
),您会尝试写入该文件(fprintf(filenew,"%s",textwrite);
)。你需要先打开它才能写作。
当scanf("%s",textwrite);
是未初始化的指针并且它指向无缓冲区时,您调用textwrite
,它应该是一个数组或指向已分配内存的指针(例如,通过malloc)。您的代码中存在以下变量时出现此错误:
在你通过之后,我认为大部分问题将被遗忘。
答案 1 :(得分:2)
strcmp
进行比较,返回-1,0或1(0表示相等)。还有stricmp
进行不区分大小写的比较。&
。答案 2 :(得分:1)
char *textwrite;
...
scanf("%s",textwrite);
您从未为textwrite
分配内存。你应该尝试像
textwrite = malloc(sizeof(char) * 128);
scanf("127%s" , textwrite);
我不知道这是否真的是你的问题。
答案 3 :(得分:0)
要停止崩溃,请不要传入firstchoice
的指针,传入变量本身。
scanf("%s",firstchoice);
对于数组,您不需要使用scanf
之类的函数的指针。您也可以使用其他scanf
执行此操作。
代码有几个问题我不会详细说明,因为这显然是一个学习练习。首先,摆脱goto
。
答案 4 :(得分:0)
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