why if I redirect the STD_OUT on a file and write to STD_OUT the file remains empty?

Viewed 70

I can't solve a problem with standard output laugh, I'm on Unix operating system, so the file-descriptor of STD_IN = 0, STD_OUT = 1, STD_ERR = 2, basically I tried to close the descriptor associated with the standard output, and then overwrite it with that of the file I want to write to, but when I open the file it is empty. CODE:

#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
/*special file's descriptor  --use it or constants in unistd.h-- */
#define STD_IN 0 /*standard input stream*/
#define STD_OUT 1 /*standard output stream*/
#define STD_ERR 2 /*standard output-error stream*/

int main(unsigned int num_of_args, char** args)
{
    if(num_of_args != 3)
    {
        write(STD_ERR, "Few argouments.\nThe use  = ./executable <message> <file>\n", 69);
        return -1;
    }

    int file_des= open(args[2],  O_CREAT,  0640);

    if(file_des < 0)
    {
        write(STD_ERR, "Error, we couldn't open file.\n", 31);
        return -1;
    }

    //I close the descriptor associated with STD_OUT
    close(STD_OUT);

    //I copy the descriptor associated with fil_des on the first available descriptor(so STD_OUT)
    dup(file_des);

    write(STD_OUT, args[1], sizeof(args[1]));

    close(file_des);

    return 0;
}

It should be fine, I don't see any errors, but when I open the file I don't find the message written. Because? help me

2 Answers

This code is incorrect:

int file_des= open(args[2],  O_CREAT,  0640);

Per the POSIX documentation for open() (note the bolded portion - my bolding):

SYNOPSIS

#include <sys/stat.h>
#include <fcntl.h>

int open(const char *path, int oflag, ...);
int openat(int fd, const char *path, int oflag, ...);

DESCRIPTION ...

Values for oflag are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>. Applications shall specify exactly one of the first five values (file access modes) below in the value of oflag:

O_EXEC
    Open for execute only (non-directory files). The result is unspecified if this flag is applied to a directory.
O_RDONLY
    Open for reading only.
O_RDWR
    Open for reading and writing. The result is undefined if this flag is applied to a FIFO.
O_SEARCH
    Open directory for search only. The result is unspecified if this flag is applied to a non-directory file.
O_WRONLY
    Open for writing only.

Your open() call needs to include either O_WRONLY or O_RDWR as you are writing to the file:

int file_des= open(args[2],  O_CREAT | O_WRONLY,  0640);

The Problem

The problem here is that the file descriptor which is being assigned to file_des isn't really 1.

int test = dup(file_des);
fprintf(stderr, "%d", test);

3

Now, what could the reason be?

The problem is with how you are opening your file. O_CREAT creates a file if it does not exist.

There is no specification whether you are opening the file for read, write, or read/write.

Since you are associating your file to the file descriptor of stdout, you must use the O_WRONLY flag as well.

int file_des = open(args[2],  O_CREAT | O_WRONLY,  0640);
Related