C++ random video selector.

I'm trying to write a program that will select and open a .mkv file in vlc media player. I have been using the rand() function, along with getline() to select a random line from a text file I created that has the directory of each .mkv listed in sequential order. everything works I even used system("filename.mkv") to see if cmd could launch it, which it can. My question is how to I get my program to place the directory location that is retrieved by getline(), into system("--randomly selected video dir location--")? Someone please help. I am stumped here...

if Necessary Ill add my code, but I think my question is fairly straight forward and I don't have access to my source code at this moment. (Typing this up at work! :P )
To pass a std::string to system(...) you need the c_str() function. See:

http://www.pickinpatchfarm.com/reference/string/string/c_str/
Thank you sir! I haven't tried it yet (at work again...) but Ill let you know if it works! I have been stumped on this for a week! You are a gentleman and a scholar!
Sooo... I'm not sure where or how I should use the function c_str(). Here is my code so far:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int random = 0;
int numOfLines = 0;

int main()
{
string line;

fstream myFile("C:\\Users\\Owner\\Desktop\\videolist.txt");

srand(time(0));
random = rand() % 50;

while (getline(myFile, line)){
++numOfLines;
if(numOfLines == random)
{
line;


}



}

return 0;
}

the text file named "videolist" just contains the following lines:

system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");
(over and over again...)

Please help... as much detail in your answer will be greatly appreciated. Don't forget, I'm a super noob. :/
Last edited on
I figured it out thanks for your help :D
This is my finished code if anyone else wants to make a random video selector:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdlib>
#include <time.h>
#include <string>

using namespace std;

int random = 0;
int numOfLines = 0;

int main()
{


string line;

ifstream myFile("C:\\Users\\Owner\\Documents\\DEV\\videolist.txt");

srand(time(0));
random = rand() % (1 - 20);

while (getline(myFile, line)){
++numOfLines;
if(numOfLines == random)
{
system(line.c_str());
}

}

return 0;
}

I plan on using this program in conjunction with windows task scheduler to use random episodes of my favorite family shows from the 90's as an alarm clock! Maybe someone else can find a similar use. Probably a better, faster, easier way to write this, but at least this method seems to be working! :)
I would suggest to add a break:
1
2
3
4
5
if(numOfLines == random)
{
system(line.c_str());
break; // Note
}
So that the loop ends as soon as you found the appropriate.

Are you sure that a string like this

system("C:\\Users\\Owner\\Documents\\c++\\videos\\bunny.mkv");

within the videolist.txt file works? This is code not data.
Registered users can post here. Sign in or register to post.
  • 452471514 2018-02-18
  • 5299351513 2018-02-18
  • 3036511512 2018-02-18
  • 4224301511 2018-02-18
  • 6975481510 2018-02-18
  • 8259761509 2018-02-18
  • 7007851508 2018-02-18
  • 7796301507 2018-02-18
  • 567811506 2018-02-18
  • 2107511505 2018-02-18
  • 8923811504 2018-02-17
  • 6039601503 2018-02-17
  • 8788931502 2018-02-17
  • 4539021501 2018-02-17
  • 661851500 2018-02-17
  • 9152461499 2018-02-17
  • 4757661498 2018-02-17
  • 981131497 2018-02-17
  • 4385321496 2018-02-17
  • 5046771495 2018-02-17