windows,c++:将文件发送到exe(c++解决方案),并从发送的文件中读取数据
windows, c++: send file to exe (c++ solution) and read data from sent file
我的目标是将任意文本文件发送到作为c++项目构建的exe。在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{User}\AppData\Roaming\Microsoft\Windows\SendTo
问题:
我想右键单击任意文本文件并将其(SendTo)传递到exe。如何将发送文件的路径作为参数传递给应用程序,以便应用程序可以读取发送的文件?
当路径作为参数传递时,我想代码行应该是这样的:
ifstream readFile(argv[1]);
非常感谢!
David
无论使用SendTo
还是OpenWith
,单击的文件名将作为命令行参数传递给可执行文件。因此,argv[]
数组将包含文件名(在argv[1]
处,除非SendTo
快捷方式指定了其他命令行参数,在这种情况下,您必须相应地调整argv[]
索引)。
我刚刚用SendTo
做了一个测试,argv[1]
运行良好。只需确保在尝试打开文件名之前检查argc
,例如:
int _tmain(int argc, _TCHAR* argv[])
{
if (argc > 1)
{
std::string readLineFromInput;
std::ifstream readFile(argv[1]);
if (readFile)
std::getline(readFile, readLineFromInput);
std::ofstream newFile(_T("output.txt"));
if (newFile)
newFile << readLineFromInput << "n";
}
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到