System() Open file error?

I am currently working on my own little version of run. I am trying to make my program open whatever file I input that is in my My Documents folder. I have windows 7, username Michael:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char** argv) {
    string command;
    string path = "C:\\Users\\Michael\\Documents\\";
    system("TITLE Run:");
    
    Start:
    cout << ">>";
    cin >> command;
    if (command == "doc"){
        string item;
        cout << ">>";        
        cin.ignore(1, '\n');
        getline(cin,item);
        path = path + item;
        cout << path;
        system(path.c_str());
        system("PAUSE");
        return 0;
    }

}

I am trying to open the file: Driving Hours.xlsx but it has a space. I am using getline(cin,item);, but it appears the system() command isn't getting things that are after spaces. This is the output:
>>doc
>>Driving Hours.xlsx
C:\Users\Michael\Documents\Driving Hours.xlsx'C:\Users\Michael\Documents\Driving' is n
ot recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .

How do I get it to open the file? Thanks in advance.


EDIT 1:
The program will open files without spaces just fine.
Last edited on
line 23 where it opens the file... its just putting the file into the command prompt. you are missing the command to open what i think are excel spread sheets

edit: try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
	string path="Whatever your path is";
	string sysCommand;
	string docType;

	cout<<">>";
	cin>> docType;

	if(docType == "txt")
		sysCommand=docType+path;

	//do that for all of the other file types
	//you want

	else
		cout<<"Invalid file type\n";

	system(sysCommand);
	system('pause');
}
Last edited on
Topic archived. No new replies allowed.