比较两个文件的第二个字节时出现分段错误
Segmentation fault in comparing the second byte of two files
我正在测试命令 fread() 和 fseek,我制作了一个程序来比较两个程序的第二个字节。该程序在指示的行(最后一个 fread())给出了分段错误。
int main(int argc, char** argv)
{
FILE *file1, *file2;
long size1, size2;
long size1a, size2a;
char *name1, *name2;
char *temp1, *temp2;
if (argc != 3)
{
cout<<"Enter two file names"<<endl;
exit(1);
}
name1 = argv[1];
name2 = argv[2];
//open files
file1 = fopen(name1, "r");
file2 = fopen(name2, "r");
//get file size
fseek(file1, 0, SEEK_END);
size1 = ftell(file1);
rewind(file1);
fseek (file2,0,SEEK_END);
size2 = ftell(file2);
rewind(file2);
fseek(file1,1,SEEK_SET);
fseek(file2,1,SEEK_SET);
cout<<"1"<<endl; //----cout worked
fread(temp1,1,1,file1);
cout<<"2"<<temp1<<endl; //---cout worked. 2nd byte of file1 was printed.
fread(temp2,1,1,file2); //SEGMENTATION FAULT AT THIS LINE
if(*temp1==*temp2)
cout<<"same"<<endl;
else
cout<<"different"<<endl;
return 0;
}
尽管我通过定义
更正了程序
char temp1, temp2;
和写作
fread(&temp1,1,1,file1);
fread(&temp2,1,1,file2);
我仍然不知道是什么导致了第二个 fread 的段错误,而第一个 运行 正确。分段错误的原因可能是什么?
1) 以二进制模式打开文件 `fp = fopen(name, "rb");
2) 查看文件打开结果if(fp != NULL)
3) 为数据分配内存:char *temp1;
是不够的,它只是指针,你需要这样的东西:
temp = (char*) malloc(sizeof(char) * N); // N number of bytes
编辑:
4) 不要忘记关闭文件(即使它不是分段错误的原因)。
我只是做了一些修改,下面的代码没有问题:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
FILE *file1, *file2;
long size1, size2;
long size1a, size2a;
long greater;
char *name1, *name2;
char *temp1, *temp2;
// memory allocation (two lines added)
temp1 = (char*) malloc(sizeof(char));
temp2 = (char*) malloc(sizeof(char));
if (argc != 3)
{
cout<<"Enter two file names"<<endl;
exit(1);
}
name1 = argv[1];
name2 = argv[2];
//open files
file1 = fopen(name1, "rb");
if(file1 == NULL)
{
cout<<"File "<< name1 << " cannot be read" <<endl;
exit(1);
}
file2 = fopen(name2, "rb");
if(file2 == NULL)
{
cout<<"File "<< name2 << " cannot be read" <<endl;
exit(1);
}
//get file size
fseek(file1, 0, SEEK_END);
size1 = ftell(file1);
rewind(file1);
fseek (file2,0,SEEK_END);
size2 = ftell(file2);
rewind(file2);
if(size1>size2) greater = size1;
else greater = size2;
fseek(file1,1,SEEK_SET);
fseek(file2,1,SEEK_SET);
cout<<"1"<<endl; //----cout worked
fread(temp1,1,1,file1);
cout<<"2"<<temp1<<endl; //---cout worked. 2nd byte of file1 was printed.
fread(temp2,1,1,file2); //SEGMENTATION FAULT AT THIS LINE
if(*temp1==*temp2)
cout<<"same"<<endl;
else
cout<<"different"<<endl;
fclose(file1);
fclose(file2);
// memory de-allocation (two lines added)
free(temp1);
free(temp2);
return 0;
}
EDIT2:
5) 在使用后取消分配内存(添加了两行)
第一个版本的问题是 temp
指针没有指向任何东西:
char *temp1, *temp2;
...
fread(temp1,1,1,file1);
有了这个,文件中的字符被放置在 temp
指针指向的随机位置。
在第二个版本中,字符放在 temp
变量中,没问题。
我正在测试命令 fread() 和 fseek,我制作了一个程序来比较两个程序的第二个字节。该程序在指示的行(最后一个 fread())给出了分段错误。
int main(int argc, char** argv)
{
FILE *file1, *file2;
long size1, size2;
long size1a, size2a;
char *name1, *name2;
char *temp1, *temp2;
if (argc != 3)
{
cout<<"Enter two file names"<<endl;
exit(1);
}
name1 = argv[1];
name2 = argv[2];
//open files
file1 = fopen(name1, "r");
file2 = fopen(name2, "r");
//get file size
fseek(file1, 0, SEEK_END);
size1 = ftell(file1);
rewind(file1);
fseek (file2,0,SEEK_END);
size2 = ftell(file2);
rewind(file2);
fseek(file1,1,SEEK_SET);
fseek(file2,1,SEEK_SET);
cout<<"1"<<endl; //----cout worked
fread(temp1,1,1,file1);
cout<<"2"<<temp1<<endl; //---cout worked. 2nd byte of file1 was printed.
fread(temp2,1,1,file2); //SEGMENTATION FAULT AT THIS LINE
if(*temp1==*temp2)
cout<<"same"<<endl;
else
cout<<"different"<<endl;
return 0;
}
尽管我通过定义
更正了程序char temp1, temp2;
和写作
fread(&temp1,1,1,file1);
fread(&temp2,1,1,file2);
我仍然不知道是什么导致了第二个 fread 的段错误,而第一个 运行 正确。分段错误的原因可能是什么?
1) 以二进制模式打开文件 `fp = fopen(name, "rb");
2) 查看文件打开结果if(fp != NULL)
3) 为数据分配内存:char *temp1;
是不够的,它只是指针,你需要这样的东西:
temp = (char*) malloc(sizeof(char) * N); // N number of bytes
编辑:
4) 不要忘记关闭文件(即使它不是分段错误的原因)。
我只是做了一些修改,下面的代码没有问题:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
FILE *file1, *file2;
long size1, size2;
long size1a, size2a;
long greater;
char *name1, *name2;
char *temp1, *temp2;
// memory allocation (two lines added)
temp1 = (char*) malloc(sizeof(char));
temp2 = (char*) malloc(sizeof(char));
if (argc != 3)
{
cout<<"Enter two file names"<<endl;
exit(1);
}
name1 = argv[1];
name2 = argv[2];
//open files
file1 = fopen(name1, "rb");
if(file1 == NULL)
{
cout<<"File "<< name1 << " cannot be read" <<endl;
exit(1);
}
file2 = fopen(name2, "rb");
if(file2 == NULL)
{
cout<<"File "<< name2 << " cannot be read" <<endl;
exit(1);
}
//get file size
fseek(file1, 0, SEEK_END);
size1 = ftell(file1);
rewind(file1);
fseek (file2,0,SEEK_END);
size2 = ftell(file2);
rewind(file2);
if(size1>size2) greater = size1;
else greater = size2;
fseek(file1,1,SEEK_SET);
fseek(file2,1,SEEK_SET);
cout<<"1"<<endl; //----cout worked
fread(temp1,1,1,file1);
cout<<"2"<<temp1<<endl; //---cout worked. 2nd byte of file1 was printed.
fread(temp2,1,1,file2); //SEGMENTATION FAULT AT THIS LINE
if(*temp1==*temp2)
cout<<"same"<<endl;
else
cout<<"different"<<endl;
fclose(file1);
fclose(file2);
// memory de-allocation (two lines added)
free(temp1);
free(temp2);
return 0;
}
EDIT2:
5) 在使用后取消分配内存(添加了两行)
第一个版本的问题是 temp
指针没有指向任何东西:
char *temp1, *temp2;
...
fread(temp1,1,1,file1);
有了这个,文件中的字符被放置在 temp
指针指向的随机位置。
在第二个版本中,字符放在 temp
变量中,没问题。