在C中使用打开,写入和关闭系统调用

时间:2017-11-15 22:09:29

标签: c

我在C中非常基本地使用Open,Write和Close系统调用时遇到问题,以下代码正在编译,但显示写入文件的0字节,并且没有内容输出到文件中。我做错了什么?!

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

int main(void)
{
    int fd;
    ssize_t numWritten;

    if(fd = open("test.txt", O_RDWR | O_CREAT | O_APPEND) == - 1)
        printf("There has been an error opening the file\n");
    else
    {
        if(numWritten = write(fd, "Test Text\n", 10) == -1)
            printf("There has been an error writing to the file!\n");
        else
            printf("You have written %ld bytes\n", (long)numWritten);
    }

    close(fd);

    return 0;
}

0 个答案:

没有答案