943,915 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 473
  • C++ RSS
Sep 16th, 2009
0

Problem opening file using system() on Windows.

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jdam7459 is offline Offline
12 posts
since Jun 2007
Sep 16th, 2009
0

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

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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 16th, 2009
0

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

To display the file contents in your command window, you need the command "type". See the following example use.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 16th, 2009
0

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

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

It turns out that for some reason the
C++ Syntax (Toggle Plain Text)
  1. system("cd Result Sets");
isn't changing the directory at all.
I added
C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jdam7459 is offline Offline
12 posts
since Jun 2007
Sep 16th, 2009
0

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

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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 16th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Salem ...
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:
C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jdam7459 is offline Offline
12 posts
since Jun 2007
Sep 16th, 2009
-1

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

Never use system() on Windows.
Use Shell or Kernel apis.
Reputation Points: -76
Solved Threads: 14
Junior Poster
marco93 is offline Offline
132 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: SSales Tax Calculator Class
Next Thread in C++ Forum Timeline: C++ array in a ascending order





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC