简体   繁体   中英

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.

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]) .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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