比较两个文件的第二个字节时出现分段错误

时间:2015-02-21 11:54:52

标签: c++ fread fseek

我正在测试命令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中给出了seg错误,而第一个运行正确。可能是分段错误的原因是什么?

2 个答案:

答案 0 :(得分:0)

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)使用后取消分配内存(再添加两行)

答案 1 :(得分:0)

第一个版本的问题是temp指针并不指向任何内容:

char *temp1, *temp2;
...
fread(temp1,1,1,file1);

有了这个,文件中的字符被放置在temp指针所指向的随机位置。

对于第二个版本,字符放在temp变量内,这没关系。