读书人

main()的参数有关问题

发布时间: 2012-02-22 19:36:55 作者: rapoo

main()的参数问题
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
#include <stdexcept>
using namespace std;

//定义函数 open_file();
ifstream &open_file(ifstream &in,char *file){
in.close();
in.clear();
in.open(file);
return in;
}

int main(int args,char *argv[]){
ifstream temp_file;
map <string,string> temp_map;
if(!open_file(temp_file,argv[1]))
throw runtime_error( "wrong open temp.txt ");
string k,v;
while(temp_file> > k> > v)
temp_map.insert(make_pair(k,v));
ifstream task_file;
if(!open_file(task_file,argv[2]))
throw runtime_error( "wrong open task.text ");
string line,key,word;
while(getline(task_file,line)){
istringstream str(line);
while(str> > key){
map <string,string> ::iterator it=temp_map.find(key);
if(it!=temp_map.end())
word=it-> second;
else word=key;
cout < <word < < " ";
}
cout < <endl;
}
return 0;
}


上面的程序运行时提示:This application has requested the runtime to terminate it in an unusual way.(提示我这个程序要在运行时决定以某种方式运行?分析后感觉是main的参数设置的问题;

我把if(!open_file(temp_file,argv[1]))和if(!open_file(temp_file,argv[2]))
中的argv[i]改成相应文件名后就可以运行了,不过这样失去了在main()中设置参数的意义,想问下,如过想在命令行输入main()参数,即指定文件名,上面的应该怎么写?


[解决办法]
编译选项里加入执行参数
[解决办法]
楼上的说得对,带你需要的参数运行
[解决办法]
VC: project -> settings -> debug -> program arguments

读书人网 >C++

热点推荐