0

I'm having problems with very basic use of Open, Write and Close system calls in C, the following code is compiling but showing 0 bytes written to the file and no content being output into the file. What am I doing wrong?!

#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;
}
rla
  • 19
  • 1

0 Answers0