0

This program is meant to take as parameter a file, then read a string from standard input and write its length into the file, then read the content of the file (which is supposed to contain the lengths of the strings from the standard input) and write it in standard output:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define MAX_BUFF 4096

int main(int argc, char **argv)
{
    if (argc != 2)
    {
        puts("you must specify a file!");
    return -1;
    }
    int nRead;
    char buffer[MAX_BUFF], tmp;
    int fd;
        puts("write \"end\" to stop:");
    fd = open(argv[1], O_RDWR | O_CREAT | O_APPEND, S_IRWXU);
    while ((nRead = read(STDIN_FILENO, buffer, MAX_BUFF)) > 0 && strncmp(buffer,"end", nRead-1) != 0 )
            {
            if ( write(fd, &nRead, 1) < 0 )
            {
                perror("write error.");
                return -1;
            }
        }
    puts("now i am gonna print the length of the strings:");
    lseek(fd, 0, SEEK_SET); //set the offset at start of the file
        while ((nRead = read(fd, buffer, 1)) > 0)
    {
        tmp = (char)buffer[0];
        write(STDOUT_FILENO, &tmp, 1);
    }
    close(fd);
    return 0;
}

this is the result:

write "end" to stop:
hello
world
i am a script
end
now i am gonna print the length of the strings:

I tried to convert the values written in the file into char before write in standard output with no success. How am i supposed to print on standard output the lengths by using unbuffered I/O? Thank you for your replies

EDIT: i changed the read from file with this:

while((read(fd, &buffer, 1)) > 0)
          {
                  tmp = (int)*buffer;
                  sprintf(buffer,"%d:", tmp);
                  read(fd, &buffer[strlen(buffer)], tmp);
                  write(STDOUT_FILENO, buffer, strlen(buffer));
          }

but actually i have no control on the effective strlen of the string thus the output is this:

13:ciao atottti
4:wow
o atottti
5:fine
 atottti

as you can see, the strlength is correct because it consinder the newline character ttoo. Still there is no control on the effective buffer size.

Joe
  • 46,419
  • 33
  • 155
  • 245
Grugnas
  • 35
  • 6
  • Are you looking for `sprintf`? Generate the string with `sprintf`, then write it with `write`. – William Pursell Oct 11 '17 at 20:38
  • looks like values written after sprintf are compromised because they don't correspond to the length of the strings from the standard input. – Grugnas Oct 11 '17 at 21:29
  • `if ( write(fd, &nRead, 1) < 0 )` what do you intend to write here? A character? (nRead is an int) – wildplasser Oct 11 '17 at 21:41
  • yes i am supposed to write it into the file as int, then print it in standard out (thus convert it into a string) – Grugnas Oct 12 '17 at 08:06
  • You only read and write sequences of bytes. Do you want some *textual representation* of an `int` or do you want to read some binary file. In all cases, define your [file format](https://en.wikipedia.org/wiki/File_format) perhaps using some [EBNF](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) notation. Consider using [C dynamic memory allocation](https://en.wikipedia.org/wiki/C_dynamic_memory_allocation) – Basile Starynkevitch Oct 12 '17 at 08:11

0 Answers0