简体   繁体   English

在这段代码中,为什么Write()不能与Int一起使用?

[英]In this code Why Write() doesn't work with Int?

I cannot understand why this code doesnt work. 我不明白为什么这段代码行不通。 it works fine with printf but i cannot get it to work with write... 它与printf正常工作,但我无法使其与写入一起工作...

#include <stdio.h>
#include <unistd.h>

int ft_putchar(char a,char b,char c){
    write(1,&a,1);
    write(1,&b,1);
    write(1,&c,1);
    return(0);
}

int main()
{
 int x = 0;
 int y, z;

 while(x <= 9){
     y = x + 1;
     while(y <= 9){
         z = y + 1;
         while(z <= 9){
             ft_putchar(x,y,z);
             z++;
         }
         y++;
     }
     x++;
 }
    return 0;
}

there are no error outputs 没有错误输出

You need to convert ASCII to its digit equivalent before writing. 写入之前,您需要将ASCII转换为其等效digit

example 5 = '5' +'0'

As of now you are writing the ASCII values to terminal. 到目前为止,您正在将ASCII值写入终端。

int ft_putchar(char a,char b,char c){

    a += '0';
    b += '0';
    c += '0';

    write(1,&a,1);
    write(1,&b,1);
    write(1,&c,1);
    return(0);
}

i want to print them like this but in the end it should have nothing 578, 579, 589, 678, 679, 689, 789, instead of 789, it should be 789 im using c= ','; write(1,&c,1); c= ' '; write(1,&c,1); 我想像这样打印它们,但最后应该没有578, 579, 589, 678, 679, 689, 789,而不是789,应该是789 im,使用c= ','; write(1,&c,1); c= ' '; write(1,&c,1); c= ','; write(1,&c,1); c= ' '; write(1,&c,1);

You need to pass the delimiter to ft_putchar function, 您需要将定界符传递给ft_putchar函数,

int ft_putchar(char a,char b,char c, char del){

    a += '0';
    b += '0';
    c += '0';
    write(1,&a,1);
    write(1,&b,1);
    write(1,&c,1);
    write(1,&del,1);
    return(0);
}

int main()
{
 int x = 0;
 int y, z;

 while(x <= 9){
     y = x + 1;
     while(y <= 9){
         z = y + 1;
         while(z <= 9){

            if (x == 7 && y == 8 && z == 9)
             ft_putchar(x,y,z, ' ');
            else
             ft_putchar(x,y,z, ',');
             z++;
         }
         y++;
     }
     x++;
 }
    return 0;
}

When you used printf , I am guessing you used: 当您使用printf ,我猜您使用了:

printf("%d %d %d",a,b,c);

So you are explicitly telling the function to interpret the variables as numbers, and print those. 因此,您要明确告诉该函数将变量解释为数字,然后将其打印出来。 When you use write, it assumes that what you are using is a char . 使用write时,它假定您使用的是char This means this would be the same as: 这意味着它将与:

printf("%c %c %c",a,b,c);

Try that - you will see you still get blanks. 尝试一下-您将看到仍然空白。 That is because you are not interpreting the variables as characters, and so converting the numbers 1..9 to their ASCII letter value. 这是因为您没有将变量解释为字符,而是将数字1..9转换为它们的ASCII字母值。 These are not normal characters and will appear blank. 这些不是正常字符,将显示为空白。

This would be the same if you used char in main instead of int . 如果在main使用char而不是int这将是相同的。 Your best option to convert a normal integer to a the ASCII value that prints said integer is via the answer by Kiran, 最好的选择是将普通整数转换为显示该整数的ASCII值,方法是通过Kiran的答案,

myInt += '0'; //Only works for numbers than 0..9. You may has well have used char to save space.

since all ASCII characters for numbers are consecutive. 因为数字的所有ASCII字符都是连续的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM