简体   繁体   English

C 系统调用 open()

[英]C system calls open()

I am having trouble with open() .我在使用open()时遇到问题。 It always returns -1 and I don't know what's wrong with the code.它总是返回-1 ,我不知道代码有什么问题。 It keeps saying:它一直在说:

 r1: No such file or directory

but the txt file is in the same directory with the C program.但txt文件与C程序在同一目录下。

#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" .我希望它创建一个名为: "maiuscole.txt"的文件并写下其中的内容: "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.要修复,请确定您从哪个目录运行可执行文件,确保您的输入文件位于那里,并且您有权在其中创建 output 文件。

As mentioned in the comments, your assignment statement for fd2 is incorrect, since the assignment operator has lower precedence than the comparison operator.如评论中所述,您对fd2的赋值语句不正确,因为赋值运算符的优先级低于比较运算符。 For the sake of consistency, I suggest you change the code to match the style used for fd1 .为了保持一致性,我建议您更改代码以匹配fd1使用的样式。 Note that 0 is also a valid file descriptor.请注意, 0也是有效的文件描述符。

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. readwrite的第二个参数分别采用void * / const void * ,因此它掩盖了您将char *[] (衰减为char ** )传递给这些函数的错误。 That is not your intention, since a char ** can only store sizeof(char *) bytes.这不是您的意图,因为char **只能存储sizeof(char *)字节。 You intend to store up to BUFFERSIZE bytes.您打算存储最多BUFFERSIZE字节。 Change buffer to be an array of char rather than an array of char * .buffer更改为char数组而不是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.从输入文件中读取数据后, read调用的返回值是读取了多少字节。 You should only write that many bytes into your output file, not the entire buffer.您应该只将那么多字节写入 output 文件,而不是整个缓冲区。

    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.如果只读取了 10 个字节,则缓冲区中前 10 个字节之后的字节未初始化,您不应将它们写入 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 .您的输入文件可能大于BUFFSIZE
  • The call to read may return less than BUFFSIZE even if the file is larger than BUFFSIZE .即使文件大于BUFFSIZE ,对read的调用也可能返回小于BUFFSIZE
  • The call to write may return less than read1 .write的调用可能返回小于read1

Finally, a note about close .最后,关于close的说明。 You are not alone in ignoring the return value of close , it is a very common thing even in production code.忽略close的返回值的并不孤单,即使在生产代码中也是很常见的事情。 However, it is still good practice to check whether or not the close succeeded.但是,检查close是否成功仍然是一个好习惯。 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.虽然关闭无效的文件描述符是良性的,但在未来,您可能会意外关闭程序的其他部分正在使用的文件描述符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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