1

For example, I am able to write the contents of an input file to and output file with:

char buffer[1024]; // character buffer
char userInput[1024]; // for user input
char *p;
char *q;
int n;
int input_file1; // file descriptor for file 1
int input_file2; // file descriptor for file 2
int output_file; // file descriptor for output_file

    while((n = read(input_file1, buffer, sizeof(buffer))) > 0)
    {
    progress("entered first while loop");
        if((write(output_file, buffer, n)) < 0)
        {
            progress("couldn't write to output within while loop 1");
            perror(argv[3]);
            close(input_file1);
            close(input_file2);
            close(output_file);
            exit(1);
        }
    }

I also have some user input:

printf("\nEnter text then hit enter: ");
q = fgets(userInput, sizeof(userInput), stdin);

I want to append the user input to the same output file using write();

How can I do this?

----- update ---- works with

if(strcmp(argv[1], "-") == 0) // use standard-in for input file 1
    {
        progress("file 1 detected as \"-\": using std input");
        p = fgets(userInput, sizeof(userInput), stdin);
        if (write(output_file, userInput, sizeof(p)) < 0) {
            progress("write from stdin to output file failed");
            perror(argv[4]);
            exit(1);
        }
        progress("wrote from stdin to output file");
    }
0

4 Answers 4

1

just you make the same thing. but you do not have to close the file in the first while.

and the get the input from the user (stdin) and then write it to the file with write() function

q = fgets(userInput, sizeof(userInput), stdin);
write(output_file, userInput, strlen(userInput));
close(output_file);
Sign up to request clarification or add additional context in comments.

3 Comments

test.c:96: error: called object ‘userInput’ is not a function
test.c: In function ‘main’: test.c:96: warning: incompatible implicit declaration of built-in function ‘strlen’
@Chips use #include <srting.h>. you have to make some effort to finish your development
0

Assuming you have already opened the output_file for writing (and possibly written to it):

#define MAX 120
char buffer[MAX];
int len = sprintf(buffer, "Whatever you want to print in %d characters.\n", MAX);
if (write(output_file, buffer, len) < 0) {
   perror("Cannot write to file.");
   exit(1);
}

2 Comments

I tried this and it is automatically entering text and not waiting for me to enter anything. It's random garbage: ?[]Ð?t&
This answers the question "How can I write a string to a file using the low-level write() function?". It does not show you how to do other things too, like reading user input. You had that part right in your question.
0

The write() function has the prototype :

ssize_t write(int fildes, const void *buf, size_t nbyte);

On a regular file or other file capable of seeking, the actual writing of data shall proceed from the position in the file indicated by the file offset associated with fildes. Before successful return from write(), the file offset shall be incremented by the number of bytes actually written. On a regular file, if this incremented file offset is greater than the length of the file, the length of the file shall be set to this file offset.

On a file not capable of seeking, writing shall always take place starting at the current position. The value of a file offset associated with such a device is undefined.

If the O_APPEND flag of the file status flags is set, the file offset shall be set to the end of the file prior to each write and no intervening file modification operation shall occur between changing the file offset and the write operation.

Comments

0

Just open() the ouput file with mode O_APPEND

output_file = open("output", O_WRONLY | O_APPEND | O_CREAT);

this way, write will append to the file.

write(output_file, userInput, strlen(userInput));

4 Comments

test.c: In function ‘main’: test.c:96: warning: incompatible implicit declaration of built-in function ‘strlen’
You have to #include <string.h>
What is the difference between a+ and what I used: O_WRONLY|O_APPEND|O_CREAT
OMG ... sorry I was mistaken. It is indeed what you used, "a+" is php >_<# ... But on the other hand I could not know what you used in your code because you did not mention it in your question: your call to open is nowhere to be seen. And as you asked how to append, I thought you did not open it correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.