windows, c++: 将文件发送到exe(c++解决方案)并从发送的文件中读取数据
windows, c++: send file to exe (c++ solution) and read data from sent file
我的目标是将任意文本文件发送到一个 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
还是 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";
}
}
我的目标是将任意文本文件发送到一个 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
还是 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";
}
}