这是我对 C++ 的第一次实验,我对编程语言总体上是新手,如果我使用了不合适的语言,请见谅。
我基本上是在尝试编写 C++ 代码以便直接打开文件,知道文件的路径和文件名。我尝试了不同的方式,但显然有一些东西(我想非常简单)我错过了。
我从我在互联网上找到的这个例子开始(这是完美的工作,在控制台提示时输入文件路径和文件名):
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string path;
string file;
cout << "Please enter the path (location) of the file: ";
cin >> path; // gets user input for location
cout << "Please enter the file name (with extension): ";
cin >> file; // gets file name
string openString = "start " + path + "\\" + file; // the string for the command
system(openString.c_str()); // sends the command and converts from type string to constant char
return EXIT_SUCCESS;
}
然后我根据我的需要以这种方式编辑了代码:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string path;
string file;
path = "C:\\test ";
file = "test1.pdf ";
string openString = "start " + path + "\\" + file; // the string for the command
system(openString.c_str()); // sends the command and converts from type string to constant char
return EXIT_SUCCESS;
}
这里的问题是我的代码只打开 C:\test\ 驱动器而不是文件“test1.pdf”。
任何提示?
非常感谢,斯特凡诺
为了解决此问题,请更改C:\\test
为C:\\test\\
您可以访问该文件的方式。
希望这有效,如果您投反对票,请评论原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句