为了记录(我在代码中定义了int line, col;)
。
#include<stdio.h>
#include<stdlib.h>
int line, col;
int i,j,ch;
char fn[20],e,c,name[64];
FILE *fp1,*fp2,*fp;
void NewFile();
void Copy();
void Delete();
void Display();
void comparison();
void main()
{
do {
printf("\n\t\t***** TEXT EDITOR *****");
printf("\n\n\tMENU:\n\t\n");
printf("\n\t1.NEWFILE\t2.DISPLAY\t3.COPY\t\t4.DELETE\t5.COMPARISON\t6.EXIT\n");
printf("\n\tEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
NewFile();
break;
case 2:
Display();
break;
case 3:
Copy();
break;
case 4:
Delete();
break;
case 5:
comparison();
break;
case 6:
exit(0);
}
}while(1);
}
void NewFile()
{
printf("\n\tEnter the name of the file to be created: ");
scanf("%s", name);
fp1=fopen(name,"w");
printf("\n\tEnter the text and press '.' to save\n\n\t");
while(1)
{
c=getchar();
fputc(c,fp1);
if(c == '.')
{
fclose(fp1);
break;
}
}
}
void comparison()
{
printf("\n\tEnter the first file: ");
scanf("%s", name);
fp1=fopen(name,"r");
printf("\n\tEnter the second file: ");
scanf("%s", name);
fp2=fopen(fn,"r");
int *line = 0;
int *col = 0;
do{
c = getc(fp1);
e = getc(fp2);
if (c == '\n')
{
*line += 1;
*col = 0;
}
if (c != e)
printf("Files not equal");
else
*col += 1;
}while(c != EOF && e != EOF);
if (c == EOF && e == EOF)
printf("Files are equal");
else
printf("Files not equal");
}
void Display()
{
printf("\n\tEnter the file name: ");
scanf("%s",fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf("\n\tFile not found!");
fclose(fp1);
printf("\n\n\tPress any key to continue\n");
}
while(!feof(fp1))
{
c=getc(fp1);
printf("%c",c);
}
}
void Copy()
{
printf("\n\tEnter the new filename to copy: ");
scanf("%s",fn);
printf("\n\tEnter the filename from which to copy: ");
scanf("%s", name);
fp1=fopen(name,"r");
fp2=fopen(fn,"w");
while(!feof(fp1))
{
c=getc(fp1);
putc(c,fp2);
}
printf("\n\tFile has been copied successfully");
fclose(fp2);
}
void Delete()
{
printf("\n\tEnter the file name: ");
scanf("%s",fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf("\n\tFile not found!");
goto end2;
}
fclose(fp1);
if(remove(fn)==0)
{
printf("\n\n\tFile has been deleted successfully!");
goto end2;
}
else
printf("\n\tError!\n");
end2: printf("\n\n\tPress any key to continue\n");
}
错误:
Segmentation fault (core dumped)
请提出一些建议。预先感谢。
printf("\n\tEnter the first file: ");
scanf("%s", name);
fp1=fopen(name,"r");
printf("\n\tEnter the second file: ");
scanf("%s", name);
// ^^^^
fp2=fopen(fn,"r");
// ^^ (never assigned)
正确的功能代码:
#include <string.h> void comparison() { printf("\n\tEnter the first file: "); scanf("%s", name); fp1 = fopen(name, "r"); printf("\n\tEnter the second file: "); scanf("%s", fn); fp2 = fopen(fn, "r"); int pC, pE; do { pC = getc(fp1); pE = getc(fp2); if (pC == '\n') { line += 1; col = 0; } if (pC != pE) printf("Files not equal"); else col += 1; } while (pC != EOF && pE != EOF); if (pC == EOF && pE == EOF) printf("Files are equal"); else printf("Files not equal"); }
您已经全局声明了变量,这可能会给您带来不确定的行为。为了解决这个问题,我创建并替换了本地函数使用的所有变量。该代码应该对您有用。
其他错误:
在发生最严重的错误之后,您所犯的错误是您试图使用也在其他函数中使用的全局变量,它们可能会提供未定义的行为。
main()
必须返回您的代码违反的整数。feof()
永远不会给出正确的结果。避免使用它。- [
注释中描述了程序中分段错误的正确原因。