Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
windows,c++;:将文件发送到exe(c+;+;解决方案)并从发送的文件读取数据 我的目标是向EXE发送任意的文本文件,这是C++项目的构建。在C++项目中,我想读取发送到EXE的文件。因此,我认为我需要将发送文件的路径传递给应用程序(exe) 我的C++代码[正在工作!]:< /P> #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(); }_C++_File_Path_Sendto - Fatal编程技术网

windows,c++;:将文件发送到exe(c+;+;解决方案)并从发送的文件读取数据 我的目标是向EXE发送任意的文本文件,这是C++项目的构建。在C++项目中,我想读取发送到EXE的文件。因此,我认为我需要将发送文件的路径传递给应用程序(exe) 我的C++代码[正在工作!]:< /P> #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,c++;:将文件发送到exe(c+;+;解决方案)并从发送的文件读取数据 我的目标是向EXE发送任意的文本文件,这是C++项目的构建。在C++项目中,我想读取发送到EXE的文件。因此,我认为我需要将发送文件的路径传递给应用程序(exe) 我的C++代码[正在工作!]:< /P> #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(); },c++,file,path,sendto,C++,File,Path,Sendto,非常感谢 David无论您使用SendTo还是OpenWith,单击的文件名都将作为命令行参数传递给可执行文件。因此,argv[]数组将包含文件名(位于argv[1],除非您的SendTo快捷方式指定了其他命令行参数,在这种情况下,您必须相应地调整argv[]索引) 我刚刚用SendTo做了一个测试,argv[1]工作正常。在尝试打开文件名之前,请确保选中了argc,例如: int _tmain(int argc, _TCHAR* argv[]) { if (argc > 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";
  }
}
int-tmain(int-argc,_-TCHAR*argv[]
{
如果(argc>1)
{
std::字符串readLineFromInput;
std::ifstream readFile(argv[1]);
如果(读取文件)
std::getline(readFile,readLineFromInput);
std::流新文件(_T(“output.txt”);
如果(新文件)

newFile SendTo已经做到了这一点。您测试过它了吗?有什么问题吗?我没有测试过它,因为argv[1]对我来说太奇怪了。正如“Remy Lebeau”所解释的,我必须进行一些字符串处理以获得到源文件的可解释路径。因此,
ifstream readFile(argv[1])
完全符合我的要求。下一步是了解其内容并进行一些字符串处理。谢谢。是的,你说得对。它可以工作!我如何将output.txt写入读取input.txt的同一目录?我的情况是,output目录就是exe所在的目录。我试图打印argv[1]但它似乎是一个标识符,而不是源文件的完整路径(c:\foo\input.txt)。请再次检查
value将包含输入文件的完整路径和文件名。您必须编写代码来解析该值,以便将路径从文件名中分割出来,然后您可以将所需的输出文件名附加到提取的路径。这是基本的字符串处理101类内容,您应该能够搜索并找到大量教程/示例s来帮助您。如果您仍然无法理解,请发布一个新问题。此问题已按要求回答。或者只使用
boost::filesystem::path::parent\u path
。好的,我会尝试。非常感谢!
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";
  }
}