I am having trouble getting Windows to open the file "results1.txt" in the directory "Result Sets".

When I run it I get the error:
"results1.txt" is not a recognized as an internal or external command, operable program or batch file.

But when I check to see if the file is in the directory it is.
The executable is the a folder one up from the folder "Result Sets".
Here's the code.

case 14:
				fout.clear();
				file2.clear();
				out.clear();
				file2 = "results";
				filePath2 = "Result Sets\\";
				counter2++;
				out << counter2;
				file2 += out.str();
				file2 += ext2;
				filePath2 += file2;
				fout.open(filePath2.c_str());

				if (fout.fail())
					cout << endl << "File could not be created..." << endl << endl;
				else
				{
					fout << "Results for " << file << "." << endl << endl;
					list.getFront(temp);
					forList.avg(temp, counter);
					fout << "The mean value is: [ " << forList.getAvg() << " ]." << endl;					
					list.getFront(temp);
					forList.med(temp, counter);
					fout << "The median value is: [ " << forList.getMed() << " ]." << endl;
					list.getFront(temp);
					forList.min(temp);
					fout << "The minimum value is: [ " << forList.getMin() << " ]." << endl;
					list.getBack(temp);
					forList.max(temp);
					fout << "The maximum value is: [ " << forList.getMax() << " ]." << endl;
					list.getFront(temp);
					forList.q1(temp, counter);
					fout << "The first quartile is: [ " << forList.getQ1() << " ]." << endl;
					list.getFront(temp);
					forList.q3(temp, counter);
					fout << "The third quartile value is: [ " << forList.getQ3() << " ]." << endl;
					list.getFront(temp);
					list.getBack(temp2);
					forList.rng(temp, temp2);
					fout << "The range value is: [ " << forList.getRng() << " ]." << endl;
					cout << "All computations are complete, and have been written to the file " << file2 << "." << endl;
					cout << "Close " << file2 << " to continue..." << endl << endl;
				}
				fout.close();
				system("cd Result Sets");
				system(file2.c_str());
				break;

Any help would be great! Thanks!

system expects an executable command. You cannot simply place the name of a text file as the argument. If you want it to launch notepad or something to view your file, you could do that. Here is what I suggest, but have not tested or used this functionality for quite some time:

system ("notepad.exe " + file2.c_str());

If that gives you a compiler error, it will probably be on the string operation, which you can simply concatenate properly (I haven't done this in cpp since string library was introduced--LOL). Hope this helps.

To display the file contents in your command window, you need the command "type". See the following example use.

#include <iostream>
#include <dos.h>
using namespace std;

int main( )
{
	char str[] = "myfile.txt";  //put your filename here
	char cmd[50] = "type ";  //note the space at end!
	strcat( cmd, str );
	system (  cmd );

	return 0;
}

The "notepad.exe" suggestion wasn't the problem but it helped me debug a bit.

It turns out that for some reason the

system("cd Result Sets");

isn't changing the directory at all.
I added

system("dir");

After the "cd" command and it turns out it's still in the directory that the executable is in.
Any other suggestions?
Thanks for those who have already replied!

a) Your "cd" command only lasts as long as the sub-process does (and it only affects the sub-process). If you really want to change the directory in this process, you need to use the chdir() API call.

b) You already have the directory in a string, so why not try to catenate the directory and filename, say the equivalent of:
notepad somedir\somefile.txt

a) Your "cd" command only lasts as long as the sub-process does (and it only affects the sub-process). If you really want to change the directory in this process, you need to use the chdir() API call.

b) You already have the directory in a string, so why not try to catenate the directory and filename, say the equivalent of:
notepad somedir\somefile.txt

Thanks Salem!

Knowing that the command only affected sub-processes was unbelievably helpful!

Here's the working code for anyone who has this problem in the future:

case 14:
				fout.clear();
				file2.clear();
				out.clear();
				file2 = "results";
				filePath2 = "Result Sets\\";
				counter2++;
				out << counter2;
				file2 += out.str();
				file2 += ext2;
				filePath2 += file2;
				fout.open(filePath2.c_str());

				if (fout.fail())
					cout << endl << "File could not be created..." << endl << endl;
				else
				{
					fout << "Results for " << file << "." << endl << endl;
					list.getFront(temp);
					forList.avg(temp, counter);
					fout << "The mean value is: [ " << forList.getAvg() << " ]." << endl;					
					list.getFront(temp);
					forList.med(temp, counter);
					fout << "The median value is: [ " << forList.getMed() << " ]." << endl;
					list.getFront(temp);
					forList.min(temp);
					fout << "The minimum value is: [ " << forList.getMin() << " ]." << endl;
					list.getBack(temp);
					forList.max(temp);
					fout << "The maximum value is: [ " << forList.getMax() << " ]." << endl;
					list.getFront(temp);
					forList.q1(temp, counter);
					fout << "The first quartile is: [ " << forList.getQ1() << " ]." << endl;
					list.getFront(temp);
					forList.q3(temp, counter);
					fout << "The third quartile value is: [ " << forList.getQ3() << " ]." << endl;
					list.getFront(temp);
					list.getBack(temp2);
					forList.rng(temp, temp2);
					fout << "The range value is: [ " << forList.getRng() << " ]." << endl;
					cout << "All computations are complete, and have been written to the file " << file2 << "." << endl;
					cout << "Close " << file2 << " to continue..." << endl << endl;
				}
				fout.close();
				filePath2 = "notepad.exe " + filePath2;
				system(filePath2.c_str());
				break;

Thanks to everyone else too!

Never use system() on Windows.
Use Shell or Kernel apis.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.