简体   繁体   English

如何使用命令行参数替换文件中的fread()?

[英]How to replace fread() from file, with command line argument?

I have the following code: 我有以下代码:

uint8_t in[BLOCK256], out[32];
bytesread = fread( in, 1, BLOCK256, fp );

then the in variable is passed to another function. 然后将in变量传递给另一个函数。

I was trying to substitute the reading from file functionality with inserting a string as an argument to the program using strcpy(in,argv[1]) . 我试图用strcpy(in,argv[1])插入字符串作为程序的参数来替代从文件功能中读取内容。

The result I am getting though its different in those two cases. 在这两种情况下,我得到的结果虽然有所不同。 I am not sure what the problem is. 我不确定是什么问题。

int main( int argc, char **argv )
{
    #define BLOCK256 64
    FILE *fp;
    int i, j, bytesread;
    uint8_t in[BLOCK256], out[32];
    state256 S;
    blake256_test();

    for( i = 1; i < argc; ++i )
    {
        fp = fopen( *( argv + i ), "r" );
        if ( fp == NULL )
        {
            printf( "Error: unable to open %s\n", *( argv + i ) );
            return 1;
        }

        blake256_init( &S );

        while( 1 )
        {
            bytesread = fread( in, 1, BLOCK256, fp );
            if ( bytesread )
               blake256_update( &S, in, bytesread );
            else
               break;
        }

        blake256_final( &S, out );

        for( j = 0; j < 32; ++j )
           printf( "%02x", out[j] );

        fclose( fp );
    }
    return 0;
}

Instead of reading the contents of a file I want to pass a string as a command line argument. 我不想读取文件的内容,而是要传递字符串作为命令行参数。

int main(argc, argv[]){
     state256 S;
     blake_test();
     unsigned char digest[32];
     for (i=1; i < argc; i++){
          blake256_init(&S);
          blake256_update(&S,argv[i], strlen(argv[i]));
          blake256_final(&S, digest);
          print_digest(digest); /* your print loop as function, with extra newline */
     }
     return 0;
}

will do the trick (hash every commandline argument string). 将解决问题(散列每个命令行参数字符串)。 Write the print function and add string.h headerfiles etc. BTW you are not reading from stdin in either case. 编写打印函数并添加string.h文件等。顺便说一句,无论哪种情况,您都不从stdin中读取。

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

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