Error when opening a file in command prompt via program

Write your question here.

Hello,

I wanted to make a c++ program that would open the command prompt, change the directory to the Y: drive, prompt the user for what file to open, and then take that user input as string "s"(this input would be something like hello.txt) and put it into the command prompt. Which should open the file.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdio.h>
using namespace std;


int main() {


	string s;

	system("cd.. & cd.. & y:");
	cout << "Please enter the file you would like to open: ";
	getline(cin, s);
	system(s.c_str());
	return 0; 
}


I seemed to have done all the steps successfully except that when I put the file name string "s" into the command prompt I always get an error saying that no command exists. However when I remove user input entirely and just structure the code like this:


int main() {


system("cd.. & cd.. & y:hello.txt");

return 0;
}

The file opens correctly, which is great and all, but I really want user input in the program to choose which file to open. What do you guys think is the problem?


If I had to guess, when the second system is run, the directory it's working in is back to the working directory of the executable. Every call to system start a whole new shell instance.
Last edited on
change the directory to the Y: drive
system("cd.. & cd.. & y:");
Why do you go up two directories if you want to change drive?

and put it into the command prompt. Which should open the file.

Sometimes. If the type of the file is known, nowadays operative systems are supposed to start a proper program to open that file and pass this as command line parameter. Which means on different machines you’ll get different results. In some machines there could be no proper programs installed and you’d get no result at all. In general, I think you’d better instruct your code to open a specific program and pass the file as a parameter.

an error saying that no command exists
Copying and pasting the exact error codes and descriptions helps to get better answers.

A simple workaround could be specifying the path to the requested file while opening it. For example, supposing you’re in a Microsoft environment, you can find a file two directories ‘above’ adding “..\..\” to its path.

Otherwise, you could have a look at this part of the standard library:
http://en.cppreference.com/w/cpp/filesystem

Example:
hello.txt:
This is hello.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::cout << "Please enter the file you would like to open: ";
    std::string s;
    std::getline(std::cin, s);
    s = "..\\..\\" + s;
    system(s.c_str());
    return 0; 
}


On my W7 machine, this code goes up two directories and call Notepad to open hello.txt.
You're program will remain opened until the called program is working, of course.
Yeah, Repeater is right. What you'd need to do is construct the entire string containing all the commands first, and then pass that into system().

Edit: What's the point of those two cd.. commands? If you're going to specify the absolute path to the file you want to open, what purpose does changing the current working directory serve?
Last edited on
Topic archived. No new replies allowed.