Problem opening file using system() on Windows.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2007
Posts: 12
Reputation: jdam7459 is an unknown quantity at this point 
Solved Threads: 0
jdam7459 jdam7459 is offline Offline
Newbie Poster

Problem opening file using system() on Windows.

 
0
  #1
Sep 16th, 2009
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.
  1. case 14:
  2. fout.clear();
  3. file2.clear();
  4. out.clear();
  5. file2 = "results";
  6. filePath2 = "Result Sets\\";
  7. counter2++;
  8. out << counter2;
  9. file2 += out.str();
  10. file2 += ext2;
  11. filePath2 += file2;
  12. fout.open(filePath2.c_str());
  13.  
  14. if (fout.fail())
  15. cout << endl << "File could not be created..." << endl << endl;
  16. else
  17. {
  18. fout << "Results for " << file << "." << endl << endl;
  19. list.getFront(temp);
  20. forList.avg(temp, counter);
  21. fout << "The mean value is: [ " << forList.getAvg() << " ]." << endl;
  22. list.getFront(temp);
  23. forList.med(temp, counter);
  24. fout << "The median value is: [ " << forList.getMed() << " ]." << endl;
  25. list.getFront(temp);
  26. forList.min(temp);
  27. fout << "The minimum value is: [ " << forList.getMin() << " ]." << endl;
  28. list.getBack(temp);
  29. forList.max(temp);
  30. fout << "The maximum value is: [ " << forList.getMax() << " ]." << endl;
  31. list.getFront(temp);
  32. forList.q1(temp, counter);
  33. fout << "The first quartile is: [ " << forList.getQ1() << " ]." << endl;
  34. list.getFront(temp);
  35. forList.q3(temp, counter);
  36. fout << "The third quartile value is: [ " << forList.getQ3() << " ]." << endl;
  37. list.getFront(temp);
  38. list.getBack(temp2);
  39. forList.rng(temp, temp2);
  40. fout << "The range value is: [ " << forList.getRng() << " ]." << endl;
  41. cout << "All computations are complete, and have been written to the file " << file2 << "." << endl;
  42. cout << "Close " << file2 << " to continue..." << endl << endl;
  43. }
  44. fout.close();
  45. system("cd Result Sets");
  46. system(file2.c_str());
  47. break;
Any help would be great! Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 903
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 144
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: Problem opening file using system() on Windows.

 
0
  #2
Sep 16th, 2009
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:
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Problem opening file using system() on Windows.

 
0
  #3
Sep 16th, 2009
To display the file contents in your command window, you need the command "type". See the following example use.
  1. #include <iostream>
  2. #include <dos.h>
  3. using namespace std;
  4.  
  5. int main( )
  6. {
  7. char str[] = "myfile.txt"; //put your filename here
  8. char cmd[50] = "type "; //note the space at end!
  9. strcat( cmd, str );
  10. system ( cmd );
  11.  
  12. return 0;
  13. }
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 12
Reputation: jdam7459 is an unknown quantity at this point 
Solved Threads: 0
jdam7459 jdam7459 is offline Offline
Newbie Poster

Re: Problem opening file using system() on Windows.

 
0
  #4
Sep 16th, 2009
The "notepad.exe" suggestion wasn't the problem but it helped me debug a bit.

It turns out that for some reason the
  1. system("cd Result Sets");
isn't changing the directory at all.
I added
  1. 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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Problem opening file using system() on Windows.

 
0
  #5
Sep 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 12
Reputation: jdam7459 is an unknown quantity at this point 
Solved Threads: 0
jdam7459 jdam7459 is offline Offline
Newbie Poster

Re: Problem opening file using system() on Windows.

 
0
  #6
Sep 16th, 2009
Originally Posted by Salem View Post
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:
  1. case 14:
  2. fout.clear();
  3. file2.clear();
  4. out.clear();
  5. file2 = "results";
  6. filePath2 = "Result Sets\\";
  7. counter2++;
  8. out << counter2;
  9. file2 += out.str();
  10. file2 += ext2;
  11. filePath2 += file2;
  12. fout.open(filePath2.c_str());
  13.  
  14. if (fout.fail())
  15. cout << endl << "File could not be created..." << endl << endl;
  16. else
  17. {
  18. fout << "Results for " << file << "." << endl << endl;
  19. list.getFront(temp);
  20. forList.avg(temp, counter);
  21. fout << "The mean value is: [ " << forList.getAvg() << " ]." << endl;
  22. list.getFront(temp);
  23. forList.med(temp, counter);
  24. fout << "The median value is: [ " << forList.getMed() << " ]." << endl;
  25. list.getFront(temp);
  26. forList.min(temp);
  27. fout << "The minimum value is: [ " << forList.getMin() << " ]." << endl;
  28. list.getBack(temp);
  29. forList.max(temp);
  30. fout << "The maximum value is: [ " << forList.getMax() << " ]." << endl;
  31. list.getFront(temp);
  32. forList.q1(temp, counter);
  33. fout << "The first quartile is: [ " << forList.getQ1() << " ]." << endl;
  34. list.getFront(temp);
  35. forList.q3(temp, counter);
  36. fout << "The third quartile value is: [ " << forList.getQ3() << " ]." << endl;
  37. list.getFront(temp);
  38. list.getBack(temp2);
  39. forList.rng(temp, temp2);
  40. fout << "The range value is: [ " << forList.getRng() << " ]." << endl;
  41. cout << "All computations are complete, and have been written to the file " << file2 << "." << endl;
  42. cout << "Close " << file2 << " to continue..." << endl << endl;
  43. }
  44. fout.close();
  45. filePath2 = "notepad.exe " + filePath2;
  46. system(filePath2.c_str());
  47. break;

Thanks to everyone else too!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 118
Reputation: marco93 is infamous around these parts marco93 is infamous around these parts marco93 is infamous around these parts 
Solved Threads: 12
marco93 marco93 is offline Offline
Junior Poster

Re: Problem opening file using system() on Windows.

 
-1
  #7
Sep 16th, 2009
Never use system() on Windows.
Use Shell or Kernel apis.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC