0

I'm trying to write a number (randomly generated) to a file using a loop for some number of iterations. I have:

while (i++ < number) {
   n = randno();
   write(openFd, (char) n, sizeof(int)); 
}

The code compiles and runs. I can print 'n' to the screen, a file has been opened (code not shown), but there does not seem to be any data in the file when I try to open it using gedit. I want a file with one very large number of a certain length. Where am I going wrong? Any help is appreciated.

By the way, what is a true random function? Thank you.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • how random must your numbers be? Why don't you use a true random function to achieve it? – vfbsilva Jan 07 '14 at 17:23
  • 1
    You can simply cast a number `n` to a char. Were you sleeping in class? – bsd Jan 07 '14 at 17:24
  • Could you check the return status of the `write()` function. On success, it should return the number of bytes written to the file. That would confirm what was written to file. – Ketan Maheshwari Jan 07 '14 at 17:26
  • This is in C/C++, right? Can you add that detail to your Q so it's more obvious? Also add the appropriate tag(s). – slm Jan 07 '14 at 17:28
  • This should go to SO, imho. – peterph Jan 07 '14 at 17:39
  • Sorry, typo. I meant to say you can't. my fingers are freezing from the cold here. You cannot cast an int to a char and expect to see a string. Try looking at file with `od` and you'll see what is being written – bsd Jan 07 '14 at 18:34

4 Answers4

2

The prototype for write() is:

ssize_t write(int fd, const void *buf, size_t count);

Notice the second arg is a pointer value, so it is a little bit surprising that passing that a random number there (which is what you have done1) does not seg fault.

Anyway, if what you want to do is do is write the number as text, you need to put it into a string first:

int n = randno();
char buffer[16]; // Will hold any int value + `\0`
sprintf(buffer, "%d", n);

You can now use buffer with write:

write(openFd, buffer, strlen(buffer));

1. n is an int; casting it with (char) simply truncates it. Perhaps you meant (char*)&n, which will work to write out the number, but in binary, not text! I presume that's not what you want since you were examining the file in gedit.

CodeClown42
  • 11,194
  • 1
  • 32
  • 67
2

You could also use fprintf if you have a FILE*

fprintf(fp, "%d",n);
AndreDurao
  • 5,600
  • 7
  • 41
  • 61
1

By casting n to a char type you have created something that will randomly write the memory pointed to from a number between 0 to 255. Expanding what you are doing, all you would need is.

while (i++ < number) {
   int n;
   n = randno();
   write(openFd, &n, sizeof(int)); 
}

The & operator will pass a pointer to N instead of the n value...

Clarus
  • 2,259
  • 16
  • 27
0

A true random function is something that output real random numbers that are not computable.

/dev/random on linux as opposed to /dev/urandom.

For your file-write I would use ''fprintf''

Nils
  • 121
  • 7