【发布时间】:2016-07-12 18:22:20
【问题描述】:
我的目标是将任意文本文件发送到一个 exe,它是一个 c++ 项目的构建。在 c++ 项目中,我想读取发送到 exe 的文件。因此,我认为我需要将发送文件的路径传递给应用程序(exe)。
我的 c++ 代码 [正在运行!]:
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::string readLineFromInput;
ifstream readFile("input.txt"); // This is explizit.
// What I need is a dependency of passed path.
getline(readFile, readLineFromInput);
ofstream newFile;
newFile.open("output.txt");
newFile << readLineFromInput << "\n";
newFile.close();
readFile.close();
}
我的 Windows 配置:
在以下路径中,我创建了 exe 的快捷方式(构建 c++ 项目): C:\Users{用户}\AppData\Roaming\Microsoft\Windows\SendTo
问题:
我想右键单击任意文本文件并将其 (SendTo) 传递给 exe。如何将发送文件的路径作为参数传递给应用程序,以便应用程序可以读取发送的文件?
当路径作为参数传递时,我猜这行代码应该是这样的:
ifstream readFile(argv[1]);
非常感谢!
大卫
【问题讨论】:
-
SendTo 已经做到了这一点。你测试过有什么问题吗?
-
我没有测试它,因为 argv[1] 对我来说太奇怪了。正如“Remy Lebeau”所解释的那样,我必须进行一些字符串处理才能获得源文件的可解释路径。所以,
ifstream readFile(argv[1]);正是我想要的。接下来是了解其内容并进行一些字符串处理。谢谢。