-1

I am trying to use the write() function from <unistd.h> to stream text from the command line into a file. I have tried multiple things along the lines of:

while(write(STDOUT_FILENO, argv[optind], strlen(STDIN_FILENO)) != 0)
{
    write(STDOUT_FILENO, "\n", sizeof("\n"));
}

but continue to get segmentation faults or infinite loops without allowing me to enter anything. I am required to do this without using read(), the only permission allowed is write for the user. I am trying to use Ctrl + D to signal end of input.

An example of how it would run is:

./a.out file.h

This line will append to the end of the file

<ctrl + d>

Then, when I open the file it should have This line will append to the end of the file at the end of it.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    `strlen(STDIN_FILENO)`? Read this part to yourself again. – SuperStormer Feb 11 '22 at 00:48
  • 1
    what do you mean 'stream text from the command line' give an example of how you are running this program and what you think it is doing / should do – pm100 Feb 11 '22 at 00:51
  • 1
    Hint: `sizeof("\n")` does not do what you think it does. – Etienne de Martel Feb 11 '22 at 01:10
  • @EtiennedeMartel I was trying to use that one as an auto skip to next line by returning the byte size of \n so it won't add any garbage after. I looked into using struct stat but thought that was only for finding info on files – Fortnite_Pro Feb 11 '22 at 01:20
  • `\n` is 1 byte. however `sizeof("\n")` is 2. `sizeof("\n")` does not do what you think it does. Additionally, every self-respective C++ compiler will refuse to compile that `strlen()` call. Whatever C++ compiler you are using, it's buggy, and you should switch to a better C++ compiler that properly reports that line as wrong. And even if all these compilation errors are fixed, there's also the matter of the entire logic being completely broken, resulting in an infinite loop. Sounds like you might find [a good C++ book](https://stackoverflow.com/questions/388242/) to be very useful? – Sam Varshavchik Feb 11 '22 at 01:25

1 Answers1

1

You would need to read() from STDIN_FILENO into a local buffer, and then write() that buffer to STDOUT_FILENO. But you explicitly said that "I am required to do this without using read()", which means you are pretty much out of luck on this. You can't get input from STDIN without reading from it. If you can't use read(), can you use any other reading function, such as fread(), gets(), getline(), etc?

What is the point of passing in file.h an an input parameter? When you execute the command /a.out file.h, the content of file.h is not sent to your program at all. In your main() function, the string value "file.h" is provided in argv[1]. You would have to use that string to open the file yourself, such as with fopen(), then read/write that file as needed.

If you want the content of file.h to be sent to your program via STDIN, you would need to execute /a.out < file.h instead. Notice the < redirection operator.

But, it sounds like your goal is to open file.h and then append STDIN input to it, in which case you would need something more like this:

FILE *f = fopen(argv[1], "w+");
if (!f) ...

char buffer[1024];
int nRead;

fwrite("\n", 1, 1, f);
while ((nRead = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0)
{
    fwrite(buffer, 1, nRead, f);
}

fclose(f);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770