| | |
Problem opening file using system() on Windows.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2007
Posts: 12
Reputation:
Solved Threads: 0
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.
Any help would be great! Thanks!
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)
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;
•
•
Join Date: Jul 2009
Posts: 903
Reputation:
Solved Threads: 144
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:
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.
C++ Syntax (Toggle Plain Text)
system ("notepad.exe " + file2.c_str());
To display the file contents in your command window, you need the command "type". See the following example use.
C++ Syntax (Toggle Plain Text)
#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; }
"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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Jun 2007
Posts: 12
Reputation:
Solved Threads: 0
The "notepad.exe" suggestion wasn't the problem but it helped me debug a bit.
It turns out that for some reason the
isn't changing the directory at all.
I added
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!
It turns out that for some reason the
C++ Syntax (Toggle Plain Text)
system("cd Result Sets");
I added
C++ Syntax (Toggle Plain Text)
system("dir");
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
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
•
•
Join Date: Jun 2007
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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
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)
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!
![]() |
Similar Threads
- problem opening a file using dos services (Assembly)
- Changes hosting but problem in opening file (PHP)
- Problem After Opening File from Website (ASP.NET)
- Very Strange Windows File System Problem (Windows NT / 2000 / XP)
- problem in opening a file with application using java (Java)
- Problem with opening file.. (C)
- extending windows "file system" attributes (C#)
- Problem with video file timing (Windows Software)
- problem with dll file (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: SSales Tax Calculator Class
- Next Thread: C++ array in a ascending order
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






