简体   繁体   中英

C system calls open()

I am having trouble with open() . It always returns -1 and I don't know what's wrong with the code. It keeps saying:

 r1: No such file or directory

but the txt file is in the same directory with the C program.

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

#define BUFFSIZE 256

char upper(char c);

int main(void){

    int fd1, read1, fd2, write2;
    char *buffer[BUFFSIZE];

    fd1 = open("minuscole.txt", O_RDONLY);
    if (fd1 < 0) { perror("r1"); exit(1); }
    read1 = read(fd1, buffer, BUFFSIZE);
    close(fd1);

    printf("%d", read1);
    if(fd2 = open("maiuscole.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644) > 0){
        write2 = write(fd2, buffer, BUFFSIZE);
    }
    close(fd2);

    return 0;
}

I expect it to create a file called: "maiuscole.txt" and write what is in: "minuscole.txt" .

Although you say your input file is co-located with your source file, that does not mean you are also running the executable from the same location. The error message clearly indicates that you are not.

To fix, determine which directory you are running your executable from, make sure your input file is located there, and that you have permission to create your output file there.

As mentioned in the comments, your assignment statement for fd2 is incorrect, since the assignment operator has lower precedence than the comparison operator. For the sake of consistency, I suggest you change the code to match the style used for fd1 . Note that 0 is also a valid file descriptor.

fd2 = open("maiuscole.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(fd2 >= 0){
    //...

The second argument to read and write take a void * / const void * respectively, so it masks a bug you have where you are passing a char *[] (which decays to a char ** ) to those functions. That is not your intention, since a char ** can only store sizeof(char *) bytes. You intend to store up to BUFFERSIZE bytes. Change buffer to be an array of char rather than an array of char * .

char buffer[BUFFSIZE];

After you read the data from your input file, the return value of the read call is how many bytes were read. You should only write that many bytes into your output file, not the entire buffer.

    write2 = write(fd2, buffer, read1);

If there were only 10 bytes read, then the bytes after the first 10 in the buffer are uninitialized, and you should not write them into your output.

There are other cases and error checking for you to consider. I won't provide all the solutions, but you should consider that:

  • Your input file may be larger than BUFFSIZE .
  • The call to read may return less than BUFFSIZE even if the file is larger than BUFFSIZE .
  • The call to write may return less than read1 .

Finally, a note about close . You are not alone in ignoring the return value of close , it is a very common thing even in production code. However, it is still good practice to check whether or not the close succeeded. Under certain conditions, it may fail because the descriptor had already been closed before. You might be interested in knowing if that happens, since it indicates a logical error of some sort in the program. While closing an invalid file descriptor is benign, in the future, you might accidentally close a file descriptor that is now being used by some other part of the program.

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