0

This is my first C++ app. I'm using Visual C++ 2010 Express. I'm trying to write a console program that will write the line "alive" to the file alive.txt on my WD USB hard drive every 10 seconds to prevent the drive from spinning down. I want the program to prompt me for the drive letter, and then use that to tell the program where the file is located. This works:

while (true)
{
Sleep(5000);
cout << "Past sleep";
ofstream AliveFile;
AliveFile.open ("j:\\alive.txt");
AliveFile << "alive" << endl;
AliveFile.close();
} 
return 0;

But when I get the drive letter from the console input and save the path to a string and pass that to AliveFile.open it doesn't work:

string DriveLetter;
cout << "What is the drive letter for the drive you want to keep awake?" << endl;
getline(cin, DriveLetter);
cin.clear();
string Path;
Path = "\"" + DriveLetter + ":\\alive.txt\"";
cout << Path << endl;

while (true)
{
Sleep(10000);
ofstream AliveFile;
AliveFile.open ( Path );
AliveFile << "alive" << endl;
AliveFile.close();
} 
return 0;

The path comes out fine when i cout << Path so I do not understand why AliveFile.open(Path) does not work.

0xDECAFBAD
  • 117
  • 1
  • 1
  • 8

1 Answers1

1
Path = "\"" + DriveLetter + ":\\alive.txt\"";  // Why adding \ before the root. 

Try -

Path = DriveLetter + ":\\alive.txt\"; 
Mahesh
  • 34,573
  • 20
  • 89
  • 115