-1

Hey how would I be able to output the first 10 words of the text file without using functions from <stdio.h>

#include <fcntl.h> 
#include <unistd.h> 
#include <stdlib.h>

int main()
{
    int fd_to_read = open("sample.txt", O_RDONLY);
    if (fd_to_read == -1) {
        exit(1);
    }
    // ...
    close(fd_to_read);
}

I have no idea how to display the first 10 words without the use of <stdio.h>

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • What part are you stuck on? Is it the 10 words? Or how to print to `stdout` via file descriptors? – Thomas Jager Aug 21 '22 at 14:51
  • 1
    [How can I make the system call write() print to the screen?](https://stackoverflow.com/questions/3866217/how-can-i-make-the-system-call-write-print-to-the-screen) duplicate?! – sidcoder Aug 21 '22 at 15:01
  • @ThomasJager yes, i've been given a text file of words, and i'm asked to display the first 10 words of the file without the use of stdio.h. How do i "write" it out to the display? – sushitrash Aug 21 '22 at 15:21
  • Can you read one character from an open file descriptor? Can you write one character to an open file descriptor? Can you do it in a loop? Can you count words by watching non-blank to blank transitions? – n. m. could be an AI Aug 21 '22 at 15:41
  • Use write: `man 2 write` – William Pursell Aug 21 '22 at 15:43
  • @n.1.8e9-where's-my-sharem. yes,yes yes and yes – sushitrash Aug 21 '22 at 16:53
  • So what's the problem then? – n. m. could be an AI Aug 21 '22 at 16:54
  • You should think about this as two separate problems. (1) How to distinguish words? This problem has nothing directly to do with how you actually read/write the words. You could solve this part of the problem first, using ``, and then, only after getting it working, rewrite it to not use ``. – Steve Summit Sep 13 '22 at 14:55
  • (2) How do I read and write characters without using ``? As others have commented, the lower-level *system calls* you want are probably [`read`](https://linux.die.net/man/2/read) and [`write`](https://linux.die.net/man/2/write). They work fine, but they're considerably less flexible, as they can only read or write simple, counted blocks of characters (not even null-terminated strings). – Steve Summit Sep 13 '22 at 14:55

2 Answers2

0

On POSIX systems the io primitives are: open, close, read, write.

These primitives operate on file descriptors rather than an FILE object as purposed by the C Standard.

Like the c io interface, the posix interface expects a buffer and a buffer size for the read and write primitives.

Assuming that words are separated by the whitespace character ' ', your job would be to read continuously from the source descriptor and count the occurrences of the space character (by iterating over the buffer char by char) until it hits the desired threshold.

Until then, write everything to the output descriptor.

In <unistd.h> you'll find these three importatnt symbols:

  • STDIN_FILENO
  • STDOUT_FILENO
  • STDERR_FILENO
Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11
0

This problem is more difficult than it looks: you cannot use <stdio.h> so you must use system calls to read from the file and write to stdout:

  • Read a byte:

      char ch;
      if (read(0, &ch, 1) != 1) {
          /* end of file reached */
          break;
      }
    
  • Write the byte to stdout:

      write(0, &ch, 1);
    
  • Testing for word boundaries is more tricky: you must skip all white space, then you have a new word until you read more white space.

chqrlie
  • 131,814
  • 10
  • 121
  • 189