これは私のC++での初めての実験ですが、私はプログラミング言語に新しいことがあります。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;
}
私のニーズに:\テスト\ドライブではなく、ファイル「test1.pdf」:
#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のみを開くことです。
ヒント?
は、ファイルにアクセスすることができる方法C:\\test\\
にこの問題を変更C:\\test
を固定するためにステファノ
'test 'の後にスペースがあるはずですか? –
返事をくれてありがとうCris、私は確信していません、私はスペースが必要だと思ったが、それは本当に私の最初の試みです。私はスペースを削除しましたが、それは同じように動作します。 – aragornii
パスの一部をまとめようとするのではなく、 'openString'のパス全体をハードコーディングしてみてください。それでも問題が解決しない場合は、コマンドプロンプトを開き、同じコマンドを実行します。おそらく、あなたはpdfファイル用のデフォルトのハンドラを持っていないでしょうか?パスとファイル名の両方から末尾のスペースを削除すると、あなたのプログラムはうまく動作します。 –