在 c++98 Linux 中读取文件名作为命令行参数并执行文件操作 "open"
To read fileName as commandLine argument and perform file Operation "open" in c++98 Linux
我正在读取 abc.cpp 文件,该文件位于 /home/documents/abc.cpp 下。要打开文件,我正在执行文件操作 open("t.open("/home/documents/abc.cpp")。我可以在其中对文件执行打开操作。
我想尝试使用命令行参数读取文件名。所以我在这里尝试的是命令行
./a.out abc.cpp ,当我编译我会抛出编译错误的代码,如何解决这个问题请帮助。
#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;
}
编译错误
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*’
t.open(file.c_str());
将解决您的问题。在 C++11 之前,唯一的函数声明是
void open( const char *filename,
ios_base::openmode mode = ios_base::in );
错误消息非常清楚地告知您:没有已知的从“std::string”到“const char*”的转换。
我正在读取 abc.cpp 文件,该文件位于 /home/documents/abc.cpp 下。要打开文件,我正在执行文件操作 open("t.open("/home/documents/abc.cpp")。我可以在其中对文件执行打开操作。
我想尝试使用命令行参数读取文件名。所以我在这里尝试的是命令行
./a.out abc.cpp ,当我编译我会抛出编译错误的代码,如何解决这个问题请帮助。
#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;
}
编译错误
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*’
t.open(file.c_str());
将解决您的问题。在 C++11 之前,唯一的函数声明是
void open( const char *filename,
ios_base::openmode mode = ios_base::in );
错误消息非常清楚地告知您:没有已知的从“std::string”到“const char*”的转换。