C++ 读取文件名作为命令行参数并执行文件操作;开放式;在c++;98 Linux

C++ 读取文件名作为命令行参数并执行文件操作;开放式;在c++;98 Linux,c++,file,dynamic,command-line-arguments,C++,File,Dynamic,Command Line Arguments,我正在阅读abc.cpp文件,该文件位于/home/documents/abc.cpp下。要打开文件,我正在执行文件操作open(t.open(“/home/documents/abc.cpp”),在这里我可以对文件执行打开操作 我想尝试使用命令行参数读取文件名,所以我在这里尝试的是命令行 ./a.outabc.cpp,将argv[1]作为文件名传递并连接字符串路径+argv[1],当我编译代码时,我将抛出编译错误,如何解决此问题请帮助 #include <iostream> #in

我正在阅读abc.cpp文件,该文件位于/home/documents/abc.cpp下。要打开文件,我正在执行文件操作open(t.open(“/home/documents/abc.cpp”),在这里我可以对文件执行打开操作

我想尝试使用命令行参数读取文件名,所以我在这里尝试的是命令行

./a.outabc.cpp,将argv[1]作为文件名传递并连接字符串路径+argv[1],当我编译代码时,我将抛出编译错误,如何解决此问题请帮助

#include <iostream>
#include <fstream> 
#include <sstream>
#include<string.h>
#include <ext/stdio_filebuf.h>

using namespace std; 
int main(int argc, char *argv[])
{
                ifstream t;
                string path = "/home/documents/";
                string file = path + argv[1];
                t.open(file);
                //t.open("/home/documents/abc.cpp");
                string buffer;
                string line;
                while(t)
                {
                        getline(t, line);
                        // ... Append line to buffer and go on
                        buffer += line;
                        buffer += "\n";
                }
                t.close();
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
IFT;
字符串路径=“/home/documents/”;
字符串文件=路径+argv[1];
t、 打开(文件);
//t、 打开(“/home/documents/abc.cpp”);
字符串缓冲区;
弦线;
while(t)
{
getline(t,line);
//…将行附加到缓冲区并继续
缓冲区+=行;
缓冲区+=“\n”;
}
t、 close();
返回0;
}
编译错误

g++ cmdLine.cpp
cmdLine.cpp: In function ‘int main(int, char**)’:
cmdLine.cpp:13:32: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
                     t.open(file);
                                ^
cmdLine.cpp:13:32: note: candidate is:
In file included from cmdLine.cpp:2:0:
/usr/include/c++/4.8.2/fstream:538:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
       open(const char* __s, ios_base::openmode __mode = ios_base::in)
       ^
/usr/include/c++/4.8.2/fstream:538:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
g++cmdLine.cpp
cmdLine.cpp:在函数“int main(int,char**)”中:
cmdLine.cpp:13:32:错误:调用“std::basic\u ifstream::open(std::string&)”时没有匹配的函数
t、 打开(文件);
^
cmdLine.cpp:13:32:注:候选人是:
在cmdLine.cpp中包含的文件中:2:0:
/usr/include/c++/4.8.2/fstream:538:7:注意:void std::basic_ifstream::open(const char*,std::ios_base::openmode)[带_CharT=char;_Traits=std::char_Traits;std::ios_base::openmode=std:_ios_openmode]
打开(常量字符*\uuuu s,ios\u基::openmode\uuuu模式=ios\u基::in)
^
/usr/include/c++/4.8.2/fstream:538:7:注意:参数1从'std::string{aka std::basic_string}'到'const char*'没有已知的转换
t.open(file.c_str());
将解决您的问题。在c++11之前,唯一的函数声明是

void open( const char *filename,
           ios_base::openmode mode = ios_base::in );
错误消息非常清楚地告诉您:没有已知的从“std::string”到“const char*”的转换。

t.open(file.c_str());
将解决您的问题。在c++11之前,唯一的函数声明是

void open( const char *filename,
           ios_base::openmode mode = ios_base::in );

错误消息非常清楚地告诉您:没有已知的从“std::string”到“const char*”的转换。

您也可以共享编译错误吗?您也可以共享编译错误吗?