【问题标题】:i am trying making a program to compare between .txt files only using system calls我正在尝试制作一个程序来仅使用系统调用来比较 .txt 文件
【发布时间】:2020-11-24 13:53:28
【问题描述】:

我有一个学校项目,作业是:

创建comp.c 以比较两个文件的内容,仅使用 系统调用:open(),read(),close()(不能使用strcmp()strncmp()strlen()comp.out a.txt b.txt comp.out 如果两个文件不相同则返回 1,如果它们相同则返回 2 要查看结果,您应该使用命令:echo $?

这是我的代码:

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc , char * argv[])
{
    if(argc != 3)
    {
        printf("something wrong with variables\n");
        exit(-1);
    }

    char* filename1 = argv[1];
    char* filename2 = argv[2];
    char* addr1;
    char* addr2;
    struct stat stat_p1;
    struct stat stat_p2;
    int fileSize1=0,fileSize2=0;

    if( stat (filename1, &stat_p1) == -1)
    {
        printf("error occurred while attempting to stat %s\n" , filename1);
        exit(-2);
    }

    if( stat (filename2, &stat_p2) == -1)
    {
        printf("error occurred while attempting to stat %s\n" , filename2);
        exit(-2);
    }

    if((fileSize1=stat_p1.st_size) != (fileSize2=stat_p2.st_size)) //checks if the size is different than its not the same file.
    {
        return 1;
    }
    else
    {
        int fd1,fd2;
        if((fd1 = open(filename1 ,O_RDONLY)) < 0)
        {
            printf("error opening file %s\n",filename1);
            exit(-3);
        }
        if((fd2 = open(filename2 ,O_RDONLY)) < 0)
        {
            printf("error opening file %s\n",filename2);
            exit(-3);
        }

        addr1 = mmap(NULL, fileSize1+ 1, PROT_READ,MAP_PRIVATE, fd1, 0);
        if (addr1 == MAP_FAILED)
        {
            printf("mmap failed\n");
            exit(-4);
        }

        addr2 = mmap(NULL, fileSize2+ 1, PROT_READ,MAP_PRIVATE, fd2, 0);
        if (addr2 == MAP_FAILED)
        {
            printf("mmap failed\n");
            exit(-4);
        }
        for(int i=0;i<fileSize1+1;i++)
        {
            if(addr1[i] != addr2[i])
            {
                return 1;
            }
        }
        close(fd1);
        close(fd2);
    }
    return 2;
}

我使用gcc -o comp.exe comp.c 编译 比我试图在 2 个文本文件之间进行比较 我运行这个命令./comp.exe 1.txt, 2.txt 它失败了:

 if( stat (filename1, &stat_p1) == -1)
{
    printf("error occurred while attempting to stat %s\n" , filename1);
    exit(-2);
}

我已经好几个小时都不知道有什么建议有什么问题 非常感谢

【问题讨论】:

  • 我的猜测是您传递的文件的绝对/相对路径无效。也许尝试打印出来并仔细检查它们是否确实正确。
  • 如果两个文件不相同则返回 1 ,并且您只检查它们的大小来确定它,if((fileSize1=stat_p1.st_size) != (fileSize2=stat_p2.st_size)) ,如果文件大小相同但它们不同怎么办.
  • 你能用perror吗?
  • int 并不总是足够大。正确的类型是off_t

标签: c linux compare


【解决方案1】:

我相信您的问题与如何调用您的程序并获得结果有关。我已经编译了它,一切都按预期工作:如果它们不同,则返回 1,如果它们具有相同的内容,则返回 2。

为了使其正常工作,您需要 a) 在 Linux 或 Windows 上启动 bash(例如使用 WSL 层)。然后输入:

$ ./fcmp fcmp.c fcmp2.c
$ echo $?
2
$ ./fcmp fcmp.c randomfile.txt
$ echo $?
1

如果您打算在“本机”Windows 中执行相同操作,那么您需要检查的变量是 ErrorLevel。所以代码是这样的:

c:> fcmp fcmp.c fcmp2.c
c:> echo %errorlevel%
2
c:> fcmp fcmp.c randomfile.txt
c:> echo %errorlevel%
1

(免责声明:由于我没有 Windows 机器,因此无法尝试。) 如果这不起作用,请尝试创建一个 BATCH 文件,例如:

@echo off
fcmp %1 %2
echo %errorlevel%

【讨论】:

  • 嘿,谢谢你的回答,我不太明白我在 ubuntu 平台上做什么,在终端上我需要做什么才能正确运行它并在 2 个 .txt 文件之间进行比较顺便说一句,在同一个文件夹中
  • 按照第一个代码块中的说明进行操作。如果你是在 Ubuntu 下工作,那么make cmp 就足以获取可执行文件,并且不需要后缀.exe
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-29
  • 2021-09-17
  • 1970-01-01
相关资源
最近更新 更多