The implementation in libc, when configured to use character device as the source, expects token to be the name of a character device that produces random numbers when read from; otherwise it expects token to be '/dev/urandom'. Defect reports. Jun 30, 2016 I wanted to open /dev/urandom, open the file-to-be-deleted for R/W, set the file's write pointer to the beginning of the file, and stream the correct number of bytes from urandom to the file. My reason for asking the original question is to try to avoid writing to.
Hi everyone and good morning :) .
i want to simply read from the linux file 'urandom' a random number and print it out on screen.just simple as that. this function should be in a file called myrandom.c ( myrandom.h must be made also, which has my function dekleration ) . i will have to compile it later to a static libray and to dynamic library, and then write a main program which prints out 5 random numbers using the static library . and another main program which prints out 5 random numbers using the dynamic library. i wrote the myrandom.c code and myrandom.h code i dont know if they are correct . i tested myrandom.c inside a main program and it gives me random number , but its very big . ist that the right way to grap a random number from the urandom file? many thanks
Recommended AnswersDev C Compiler
You're also using
printf incorrectly. When you are printing out the value of an int, printf does not want the address of that int. It wants the int itself.
printf('%d', &rNumber); is wrong.printf('%d', rNumber); is right.
![]()
Woofer 808 vst free download. WTF is going on with this?
David cook laying me low download. It's still wrong. Jump to Post
str is effectively an int* , so &str is an int** , so *(&str) is an int* . So you're doing exactly what you were doing before (wrongly providing an int* when you …
All 10 Repliessepp2k378
You need to
#include <stdio.h> to get the declaration of fopen . As it is now you're implicitly declaring fopen in a way that is incompatible with its definition. You should be getting a warning about that (and in C99 and later you should instead be getting an error that you're using a function without a declaration).
Dev Urandom In C++
Once you do that, you should be getting another warning that you're implicitly converting the result of
fopen from FILE* to int . That's because fopen doesn't read anything from the file nor does it return an int. fopen opens the file and then returns a file pointer. That file pointer can then be given to other file IO functions (like fread , fwrite , fgets , fscanf , fprintf etc.), which will actually read from or write to the file. Once you're done working with the file, you need to close it by passing the file pointer to fclose .
Read From /dev/urandom C++
So no, you're not currently doing it correctly. The number you're printing is simply the numeric value of whichever pointer was returned by
fopen . It has nothing to do with the contents of the file.
Urandom C++
Thank-you @coder777 for the input. Unfortunately, unless I'm missing something, this will not work for me. Feel free to correct me if I'm wrong.
I guess I should spell out my needs a little bit better. I want to write random data to a fixed size file as quickly as possible. The current plan is to allocate a buffer the size of a block on the file system, read from /dev/urandom into it, and write into the file. I will loop until I have written the entire required size (calculating the remainder during the last iteration). I would like to avoid the intermediate buffer and copy directly from the input file's filebuf into the output file which is what I believe is happening in the sample code I posted in my first post1. However, it appears to only copy the entire contents of the input stream into the output stream. For /dev/urandom, that will be never-ending. Using a for loop to count values copied into the output file would be more overhead than creating and deleting the buffer. Writing 1 byte (or int or long long) at a time is not the answer. (Correct me if I'm wrong). I would like to set the output filebuf's epptr, but that seems to be (understandably) inaccessible to me. Other methods of generating large quantities of random data are welcome, too. 1 There may be a hidden intermediate buffer in this scenario that I don't know about. If that is the case, this is all moot. Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |