0

I am trying to print something on the console just by using the syscall write(). So far, it is more difficult than I thought.

The function is kept simple:

void writeOutput(char a, int64_t b, uint8_t c) {
    char bNew[]; // what should be the size of that array=
    while(b != 0x0000000000000000) {
      int leftover = b%10;
      bNew[i] = intToChar(leftover);
      b = b/10;
      i++;
    }
}

char intToChar(int num) {
    return '0' + num;
}

a should be formatted as char, b as a decimal number and c as an octal number.

For example a possible input is: writeOutput('a', -0xffff, 17); // ouput is "a -65535 21"

The the printed arguments should be separated by spaces and terminated by a newline to standard output. Also, I am trying to do that without the the functions of the sprintf-family. This makes it a little bit trickier.

EDIT: My question is: How is it possible to do that with write()? (Sry, missed that!) The code is online. EDIT2: My main problem is doing it without using the sprintf-family. I hoped you might help me.

  • 2
    So, what's your question? – Kijewski Nov 18 '13 at 20:48
  • 6
    and why don't you want to use `printf()`? – clcto Nov 18 '13 at 20:50
  • Convert the arguments into strings and then call write. – Stuart Nov 18 '13 at 20:52
  • @Kay: Honestly, I have been trying to implement that, but I failed so far. I hoped, someone may help me. –  Nov 18 '13 at 20:53
  • Hints for itoa() here http://stackoverflow.com/questions/3440726/what-is-the-proper-way-of-implementing-a-good-itoa-function Look at the second answer. – Charlie Burns Nov 18 '13 at 20:55
  • @Stuart: I did try this. But I got very weird results like rectangles with question marks. –  Nov 18 '13 at 20:56
  • 2
    You could just use `sprintf` to create the string and then pass it to write. – Alex Reinking Nov 18 '13 at 20:57
  • What is wrong with sprintf? – Till Nov 18 '13 at 20:57
  • Here's another good reference. ( Be careful, some of these have bugs ) http://www.jb.man.ac.uk/~slowe/cpp/itoa.html – Charlie Burns Nov 18 '13 at 21:00
  • There isn't anything wrong with sprintf or printf. I already did this with printf. But I was also trying to do that with write(). –  Nov 18 '13 at 21:01
  • If you show the code that you wrote to do this, maybe you will get some advice. – Stuart Nov 18 '13 at 21:10
  • My main problem is to that without the sprintf-family. That's why I am asking you for help. –  Nov 18 '13 at 21:23
  • Not using any of the members of the `printf()`-family of functions would force you to write your own format parser, which is not a simple task. If it's however only for `%c` (`char`), `%ld` (`long signed int`) and `%hhu` (`unsigned char`) it might be feasable. – alk Nov 18 '13 at 21:25
  • @alk: When I declare char bNew[10] I get the error that it is undeclared. Also an error which says: conflicting types for 'intToChar'. Do you know why? –  Nov 18 '13 at 21:39
  • You declare `bNew` as `char[]` which is quiet unspecific. You' d need a `char[19+1]` to store a signed 64bit integer as a C-"string", as the maximum possible value is `9223372036854775807` plus the necessary `0`-terminator. Btw: the `while` in your source snippet seems to be missing the braces. – alk Nov 18 '13 at 21:47
  • Just change `char bNew[];` to be `char bNew[19+1] = "";` – alk Nov 18 '13 at 21:54
  • @alk: You are right. This helped to get rid of the error. But I still get the error message: conflicting types for 'intToChar' Do you know that also? –  Nov 18 '13 at 21:55
  • Change `int leftover = b%10;` to be `char leftover = b%10;` and `char intToChar(int num) ` to be `char intToChar(char num)`. – alk Nov 18 '13 at 22:27
  • @alk: Unfortunately, it did not help. I still get the same error. –  Nov 18 '13 at 22:47
  • Why do I get this error message anyway? –  Nov 18 '13 at 23:02
  • It seems the code misses a prototype for `intToChar()`. Add `char intToChar( num);` before its 1st usage, that is at least before the declaration of `writeOutput()`. – alk Nov 19 '13 at 06:06
  • @alk: Thanks alk. This was causing the error message. –  Nov 20 '13 at 00:18

2 Answers2

1

you can call the write() with first argument which is an fd as stdout. it will write to the stdout and which is your console. but before that you need have a string buffer for that you can use sprintf' orsnprintfwith the same formatting asprintf`. but the printf way is easier and it should serve the purpose unless you have some very special use case.

joe
  • 1,136
  • 9
  • 17
  • The questioner specifically says he/she doesn't want to use the functions of the sprintf family. – Stuart Nov 18 '13 at 21:21
  • Of course, it possible with sprintf, but is it not possible without using functions of the sprintf-family? –  Nov 18 '13 at 21:26
  • @user2965601 you could write your own `printf` family. – clcto Nov 18 '13 at 22:08
1
#include <unistd.h> 
 
int main(void) { 
    write(1,"Hello World!", 12); 
    return 0; 
}