In my spare time I have decided I wanted to write a program that will open files no matter their names. This is the code I have written but the program doesn't seem to be able to find the file though it is in the same folder as the exe itself. Hopefully someone can find the flaw in it i didn't. I already asked two of my friends who know c++ and they don't have a clue.
Code:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
void main()
{
srand((unsigned)time(0));
int random_integer;
string dir = "";
string txt = ".txt";
string file;
string str = "";
char c;
ifstream myFile;
do {
string str = "";
for(int index=0; index<1; index++)
{
random_integer = (rand()%25)+65;
c = (char)random_integer;
str += c;
}
file = str;
file.insert (1,txt);
myFile.open(file.c_str());
if (!myFile) {
cout << "Unable to open " << file << endl;
}
} while (!myFile);
cout << "File open";
}
Comment