linux 标准输入、标准输出管道

linux stdin, stdout pipe

我有一个brick.sprite。 我在 Debain 8 "Kali Linux" 中有一个可执行文件,代码如下:

#include <stdio.h>
#include <stdint.h>
#include <iostream>

/**
  * To use this file, pipe a sprite of the old format into stdin, and
  * redirect stdout to a second file of your chosing.  The sprite header
  * will be converted.  This tool has no error checking and assumes a valid
  * sprite header.  It is provided merely for convenience.
  */

int main( int argc, char *argv[] )
{
    uint8_t zero = 0;
    uint8_t val;
    int ret;

    /* Read in old width */
    ret = fread( &val, sizeof( val ), 1, stdin );
    /* Write empty value and then new */
    fwrite( &zero, sizeof( zero ), 1, stdout );
    fwrite( &val, sizeof( val ), 1, stdout );
    
    /* Read in old height */
    ret = fread( &val, sizeof( val ), 1, stdin );
  

    /* Write empty value and then new */
    fwrite( &zero, sizeof( zero ), 1, stdout );
    fwrite( &val, sizeof( val ), 1, stdout );

    /* Straight copy of bitdepth and format */
    ret = fread( &val, sizeof( val ), 1, stdin );
    fwrite( &val, sizeof( val ), 1, stdout );

    ret = fread( &val, sizeof( val ), 1, stdin );
    fwrite( &val, sizeof( val ), 1, stdout );

    /* Assuming horizontal and vertical stride of 1 */
    val = 1;
    fwrite( &val, sizeof( val ), 1, stdout );
    fwrite( &val, sizeof( val ), 1, stdout );
 
 printf("%d\n",ret);//set to avoid weird error

    /* Now just byte copy until end of stream */
    while( !feof( stdin ) )
    {
        ret = fread( &val, sizeof( val ), 1, stdin );
        
        if( !feof( stdin ) )
        {
            /* Only copy out if the last read didn't make an eof */
            fwrite( &val, sizeof( val ), 1, stdout );
        }
    }

    return 0;
}

将 brick.sprite 转换为新格式。 我尝试了很多代码:

//convtool is the executable
convtool grep <brick.sprite date > brick2.sprite 
convtool <brick.sprite> brick2.sprite //This looks like that it goes in the right way...
convtool cat <brick.sprite> brick2.sprite
convtool 2> brick2.sprite > brick.sprite

我不熟悉 linux 但我需要知道。

谢谢指教!

如果您打算将文件 "brick.sprite" 的内容重定向到 "convtool" 的标准输入,您可以使用 < 运算符。

convtool < brick.sprite

另一种方法是将内容 cat(将文件内容写入标准输出)并将其通过管道传输到您的工具中。

cat brick.sprite | convtool

看来您还想将工具的输出重定向到新文件 brick2.sprite。将输出重定向到文件是由 > 运算符完成的。

要完成上面的两个变体:

convtool < brick.sprite > brick2.sprite
cat brick.sprite | convtool > brick2.sprite

第一个变体应该也适用于 windows 和 osx 外壳,它不是 linux 特定的。

一些提示:

// This is complete nonsense. 
// you use the name of linux tools/commands as argument of convtools.
convtool grep <brick.sprite date > brick2.sprite

//This looks like that it goes in the right way...
// this is one of the right ways ;-)
convtool < brick.sprite > brick2.sprite

// your convtool does not use arguments.
// But i think this would work too. "cat" is nonsense.
convtool cat <brick.sprite> brick2.sprite

// after performing this command you have written the eeror output stderr 
// to your brick2.sprite file and you have overwritten your 
// brick.sprite file with the std out. (I hope you have a copy ;-)
convtool 2> brick2.sprite > brick.sprite