简体   繁体   中英

when I try to read from txt file it returns : Cannot read input file

I am trying to write a program that compares 2 files and return if their equal or not.

I can only use the functions: fork, dup, dup2, open, write, exec, read.

When I compile the program on linux gcc, it returns Cannot read input file

shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out input.txt input.txt Cannot read input file

the code:

/*
* This function checks if the files are similar or similar by case    sensitive
 * it gets 2 files, and returns: 3 if identical, 2 if identical but only if not
 * case sensitive or 1 else.
 */
int CheckSimilar(char *path1, char *path2){

//open the files
int fd1 = open(path1, O_RDONLY), fd2 = open(path2, O_RDONLY);
int flag = 1;//this flag is to check for case sensitive
char *firstFile = NULL, *secondFile = NULL;
int readBytes, read2ndFile;

if (fd1 == -1 || fd2 == -1){
    write(2, "Cannot open input file\n", 24);
    return -1;//checks if there is a problem opening the file
}

while (1){

    readBytes = read(fd1, firstFile, 1);
    read2ndFile = read(fd2, secondFile, 1);

    if (readBytes < 0 || read2ndFile < 0){
        write(2, "Cannot read input file\n", 24);
        return -1;
    }//checks if there is a problem reading chars from the file

    if (!readBytes || !read2ndFile)
        break;

    if (*firstFile == *secondFile)
        continue;//the chars are equal
    //checks if it's an abc char
    else if ((*firstFile > 64 && *firstFile < 91) ||
            (*firstFile > 96 && *firstFile < 123)){
        // checks for not case sensitive
        if ((*firstFile - *secondFile) == 22 ||
                (*firstFile - *secondFile) == -22)
            flag = 0;
    }
    else
        return 1;
}

close(fd1);
close(fd2);

if (readBytes != read2ndFile)
    return 1;
if (flag)
    return 2;
return 3;
}

Make yourself a better world and ask the system about errno , and read the manual about the system calls read(2), open(2),... and errno(3)

(read(2) for example is a manual page address, saying: Manual page "read" in section 2, read man man about sections).

#include <stdio.h>
#include <string.h>
#include <errno.h>
[...]

    char* err = strerror(errno);
    char* errlen = err ? strlen(err): 0;
    char* form = "Cannot read input file since \"%s\".\n"
    if (errlen == 0) {
        form = "Cannot read input file failed with unknown error %d.\n";
        fprintf(stderr, form, errno);
    }
    else {
        fprintf(stderr, form, err);
    }

As you cannot use fprintf, I leave it for you to write the forms. At least you should print out the errno after the failed read.

The problem is here:

You declare:

 char *firstFile = NULL, *secondFile = NULL;

and then you use

read(fd1, firstFile, 1);

when firstFile is NULL , and therefore read fails.

Declare firstFile and secondFile like this:

char firstFile[1];
char secondFile[1];

检查文件是否存在于给定的路径中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM