Thread: Using write() to display an integer in the output

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    Question Using write() to display an integer in the output

    So currently just about tried everything with the best of my knowledge to get this to work but I have been hitting a wall it seems.

    Code:
    #include <stdio.h>#include <string.h>
    
    
    main(){
            int i, len;
            char name[20], length;
            unsigned x;
            char len_str[10];
    
    
            write(1,"Enter your last name: ",21);
            fflush(stdout);
            len = read(fileno(stdin),name,20);len--;name[len] = 0;
    
    
    
    
            write(1,"Name entered is ",16);
            write(1,name,len);
            write(1," length is ",11);
    
    // !!!!THIS PART!!! //
    // My attempt to work around with no luck //
            len_str[0] =  len;
            len_str[1] = 0;
            write(1,len_str, 2);
            write(1,". \n", 3);
    
    //Clearly works here//
            printf("%d\n", len);
    }
    I have tried casting write(1,(char) len, 1); tried passing it without casting, tried to make a pointer pointing to it and then passing that through but no luck empty space where the number should appear. I know that len is correct because of printf... but the goal is to not use printf just write() and not putchar, or >> and im having no luck finding any solutions.

    This is only a section of the code without my comments (sorry) just trying to find a solution to the writing issue, maybe i missed something and a source of where i can go to find the answer could be of help (or a solution ), thanks!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you're trying to output an integer's value in human-readable form with write then you'll have to translate the value to chars yourself. To print a single digit, you add it to the char '0'.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should properly convert the int to string, then write the string. At the moment, your conversion process is wrong. Possible alternatives include the use of sprintf.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Laserlight - i have the program working with printf because it was easy, but the assignment is to not use any of the printf() or sprintf()
    i suppose i just am not clear on how to go about making the int into a readable entity for the write(), from what i can tell if i pass a array of char's though it with a 0("null") indicating the end it works fine because write(1,name,len); works perfect but write(1,len_str, 2); does not when i try.

    Oogabooga- Sorry, im a C/C++ programming newbie and i dont understand waht you mean by adding it to char '0'

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Concentrate on writing a function that converts a (possibly unsigned) int to a string representation.

    The expression n % 10 will give you the value of the rightmost digit.

    Dividing the number by 10 will remove the rightmost digit.

    Using those two together you can "peel off" the digits of the number one-at-a-time from the right.

    To translate the digit to a character for output you add it to the character '0'.

    Here's an example of these concepts for you to play with:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int n = 123;
    
        putchar((n % 10) + '0');
        n /= 10;
        putchar((n % 10) + '0');
        n /= 10;
        putchar((n % 10) + '0');
    
        return 0;
    }
    The trouble with the above is that the number is printed out backwards. You'll want to put the chars (the (n % 10) + '0') into a string and then reverse it (or possibly fill it backwards). There's other details you may have trouble with but that's enough for now.

    I'm off to bed, but the others will help you!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Oogabooga - Awesome, worked perfect once I got what was happening.
    Now I have another question for you or anyone, is there anyway to compare the bits within a unsigned variable?
    Code:
    unsigned x, y;
    ... (store some char)
    (for loop 8 times)
    if (x > y){ 
    y=x>>1;}
    else
    x>>=1;
    ...
    the idea is to find the largest valued unsigned bit and shift to the left so for example; 0 0 0 0 0 0 0 1 --> shift till its at 1 0 0 0 0 0 0 0
    Or if there is no way to do compare at the bit level let me know, i have an idea of another work around but just for my knowledge

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't understand the question. Can you rephrase it?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 05-02-2012, 04:17 PM
  2. how to display name and integer
    By mkhl_2006 in forum C Programming
    Replies: 2
    Last Post: 12-31-2011, 07:57 AM
  3. How to read in an integer and display it again
    By axr0284 in forum C++ Programming
    Replies: 7
    Last Post: 12-07-2004, 01:37 PM
  4. Help me: Display 100's 10's 1's from an integer!
    By Ramza in forum C Programming
    Replies: 5
    Last Post: 03-12-2002, 10:31 PM
  5. How to display a 64-bit integer?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2002, 11:13 PM