我希望能够从命令行读取文本文件,但我的程序没有编译。有谁知道我做错了什么?
我正在尝试让命令接受menu.txt文件并读取它们并将它们放入数组但我不知道如何从命令行读取它们
所以我要做的就是
1)./a.out menu1.txt menu2.txt
让用户选择他们想要读取的文件数量,这样也可以
2)./a.out menu1.txt menu2.txt menu3.txt我该怎么做?
menu1.txt
hamburger 5.00
pizza 3.25
chips 2.50
menu2.txt
hamburger 2.00
pizza 2.35
chips 1.50
menu3.txt
hamburger 4.00
pizza 5.35
chips 0.50
这是我到目前为止所做的:
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main( int argc, char* argv[] ){
ifstream inStream;
ofstream outStream;
for(int i=1; i <= argc; i++)
// i=1, assuming files arguments are right after the executable
{
string fn = argv[i]; //filename
cout << fn;
fstream f;
f.open(fn);
//inStream.open("menu1.txt");
// inStream.open("menu2.txt");
string item[10];
double cost[10];
// ifstream m1("menu1.txt");
//ifstream m2("menu2.txt");
string name;
double var;
string line;
istringstream iss(line);
short loop=0;
if(f.is_open()){
while(!f.eof()){
iss >> name >> var;
getline(f, line);
while(f>>name>>var){
item[loop]=name;
cost[loop]=var;
cout << "Item at index " << loop << " " << item[loop]<<endl;
cout << "Cost at index " << loop << " " << cost[loop]<<endl;
loop++;
}
}
f.close();
}
}
return 0;
}
当我尝试编译时,这些是错误消息:
p3.cpp: In function ‘int main(int, char**)’:
p3.cpp:17:25: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
inStream.open(fn);
^
p3.cpp:17:25: note: candidate is:
In file included from p3.cpp:1:0:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/fstream:541: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/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/fstream:541:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
答案 0 :(得分:2)
您正在尝试将文件名作为字符串传递给fstream :: open,只需要
open(const char* __s, ios_base::openmode __mode = ios_base::in)
作为参数,您的编译器试图告诉您的内容。这意味着指向char数组的指针。因此,解析arguemtns会更好:
const char * fn = argv[i]
另一种方法是启用c ++ 11支持,因为open方法也接受std :: string作为参数: http://www.cplusplus.com/reference/fstream/fstream/open/
也可以将字符串转换为c风格的字符串并调用函数:
f.open(fn.c_str());
另外,请不要使用:
using namespace std;
因为它可能导致可怕的问题。
答案 1 :(得分:1)
您正在将argv[i]
转换为不需要的string
。你可以这样做。
const char *fn = argv[i];
// other code
f.open(fn);
fstream::open
接受const char* filename
参数。
答案 2 :(得分:0)
这是解决方案。
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main( int argc, char* argv[] ){
ifstream inStream;
ofstream outStream;
for(int i=1; i < argc; i++){
cout << endl << argv[i];
fstream f;
f.open(argv[i]);
//inStream.open("menu1.txt");
// inStream.open("menu2.txt");
string item[10];
double cost[10];
// ifstream m1("menu1.txt");
//ifstream m2("menu2.txt");
string name;
double var;
string line;
istringstream iss(line);
short loop=0;
if(f.is_open()){
while(!f.eof()){
iss >> name >> var;
getline(f, line);
while(f>>name>>var){
item[loop]=name;
cost[loop]=var;
cout << endl << "Item at index " << loop << " " << item[loop];
cout << endl << "Cost at index " << loop << " " << cost[loop];
loop++;
}
}
cout << endl;
f.close();
}
}
return 0;
}