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.cplusplus.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.
No I had to remove the (" and ") from the beginning and end of each file dir name and then it started working, I forgot to mention that in my solution. Once again thanks for your help and I didn't realize there was even a function called break that I could use! :)
Registered users can post here. Sign in or register to post.