LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-12-2004, 05:51 AM   #1
iluvatar
Member
 
Registered: Jul 2003
Location: netherlands
Distribution: debian
Posts: 403

Rep: Reputation: 30
show contents of random file?


Hello everybody!

Is it possible to show the contents of a random file in a directory? say I have a dir with 500 text files I want to give a command wich shows one file chosen randomly... can this be done with some kind of shell script?

Greetz,
.-=~ iluvatar ~=-.
 
Old 07-12-2004, 06:27 AM   #2
BluePyre
Member
 
Registered: Mar 2004
Location: London
Distribution: Mandrake 10
Posts: 172

Rep: Reputation: 30
Shell script, i'm not so sure, but here's the C++ source for something like this (i'm not near a compiler, but it should work with none or some minor changes).
It assumes that the text files are called 1, 2, 3, 4... 499, 500, but the code can be easily modified to change this.

Code:
#include <ctime>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int rangeOfNumbers(int lowest, int highest) {
    srand(static_cast<unsigned>(time(0)));
    int range = highest - lowest + 1;
    int lowest = int(range * rand()/(RAND_MAX + 1.0));
    return lowest;
}

int main() {
   char buffer[1000];
   int amountOfFiles = 500;
   int fileToOpen = rangeOfNumbers(1, amountOfFiles);
   string fileName = sprintf(fileName, "%d", fileToOpen);
   ifstream file(fileName.c_str());
   while(!file.eof()) {
      file.getline(buffer,1000);
      cout << buffer;
   }
   file.close();
   cout << "\n";
   return 0;
}
Save it as as randomfile.cpp, then compile it with:
g++ -o randomfile randomfile.cpp
Place it in the same directory as the text files, then run it.

Hope thats what you were looking for.

Last edited by BluePyre; 07-12-2004 at 06:29 AM.
 
Old 07-12-2004, 08:07 AM   #3
iluvatar
Member
 
Registered: Jul 2003
Location: netherlands
Distribution: debian
Posts: 403

Original Poster
Rep: Reputation: 30
This solutions suits fine, but I get a parse error:

Code:
randomfile.cpp: In function `int rangeOfNumbers(int, int)':
randomfile.cpp:11: declaration of `lowest' shadows a parameter
randomfile.cpp: In function `int main()':
randomfile.cpp:19: implicit declaration of function `int sprintf(...)'
randomfile.cpp:19: warning: cannot pass objects of type `string' through `...'
randomfile.cpp:19: conversion from `int' to non-scalar type `basic_string<char,string_char_traits<char>,__default_alloc_template<true,0> >' requested
I'm not a programmer, it doesn't make sense to me...

greetz,
.-=~ iluvatar ~=-.

edit: I run this GCC version:
gcc version 2.95.3 20010315 (release)
on slackware 8.1, kernel 2.4.18

Last edited by iluvatar; 07-12-2004 at 08:08 AM.
 
Old 07-12-2004, 10:38 AM   #4
BluePyre
Member
 
Registered: Mar 2004
Location: London
Distribution: Mandrake 10
Posts: 172

Rep: Reputation: 30
Okay, i'm by a compiler now.
I'll fix it up.
 
Old 07-12-2004, 10:45 AM   #5
BluePyre
Member
 
Registered: Mar 2004
Location: London
Distribution: Mandrake 10
Posts: 172

Rep: Reputation: 30
Found a couple of silly mistakes.
Here's the working version:

Code:
#include <ctime>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int rangeOfNumbers(int lowest, int highest) {
    srand(time(0));
    return ((rand() % 500) + 1);
}

int main() {
   char buffer[500];
   char fileName[9];
   int amountOfFiles = 500;
   int fileToOpen = rangeOfNumbers(1, amountOfFiles);
   sprintf(fileName, "%i", fileToOpen);
   ifstream file(fileName);
   while(!file.eof()) {
      file.getline(buffer,1000);
      cout << buffer;
   }
   file.close();
   cout << "\n";
   return 0;
}
Edit: Just made a minor change that was making the Random range number function return a zero value every time. I generated 500 files and tried the program, it works well now

Last edited by BluePyre; 07-12-2004 at 12:21 PM.
 
Old 07-13-2004, 12:40 AM   #6
wipe
Member
 
Registered: Jun 2004
Location: High Green
Distribution: Fedora Core 4
Posts: 180

Rep: Reputation: 30
Sure, this can be done with a shell script, no need to compile. A simple one-liner that should work in Bash:
Code:
cat "$(ls|sed -n $((RANDOM%$(ls|wc -l)+1))p)"
And the files can even be called whatever you want, not just 1, 2 ... 500. This method of getting a random number isn't as random as possible, so the calculation could be done with bc:
Code:
cat "$(ls|sed -n $(echo "scale=9;$RANDOM/32768*$(ls|wc -l)+1"|bc|cut -d. -f1)p)"
(that probably shouldn't be on one line ) or AWK:
Code:
cat "$(ls|sed -n $(echo|awk "{srand();print int(rand()*$(ls|wc -l)+1)}")p)"
or even Perl:
Code:
cat "$(ls|sed -n $(perl -e"print int rand($(ls|wc -l))+1")p)"
But since we are using Perl, why not write the whole thing with it for some speed:
Code:
#!/usr/bin/perl
opendir d, '.';
@list = grep ! /^\./, readdir d;
closedir d;
open f, $list[int rand(scalar @list) + 1];
while (<f>) {
	print $_;
}
close f;
Am I crazy or what?

Regards
Simon

EDIT: Do use the C++ version, It's likely fastest.

Last edited by wipe; 07-13-2004 at 12:48 AM.
 
Old 07-13-2004, 02:53 AM   #7
iluvatar
Member
 
Registered: Jul 2003
Location: netherlands
Distribution: debian
Posts: 403

Original Poster
Rep: Reputation: 30
hmmm I thought I knew the bash a bit... I'll go back to again I'll try the C++ version a.s.a.p., thanks!

greetz,
.-=~ iluvatar ~=-.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dont show window contents whilst dragging in GNOME dangerousdave Linux - Newbie 2 08-25-2005 05:08 PM
KDE 3.2 won't show CD contents spookymulder825 Linux - Software 4 03-07-2005 08:52 PM
cdrom. contents show up as question marks extrasolar Linux - General 4 01-02-2005 02:50 PM
why does screen show contents of CD but doesn't read/play CD? metallicgangsta Linux - Newbie 11 11-22-2004 01:05 AM
samba does not show folder contents Goma_2 Linux - Networking 0 12-10-2003 05:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration