string ReadLine(char *filename,int line)
{
int i=0;
string temp;
fstream file;
file.open(filename,ios::in);
if(line<=0)
{
return "Error 1: 行数错误,不能为0或负数。";
}
if(file.fail())
{
return "Error 2: 文件不存在。";
}
while(getline(file,temp)&&i<line-1)
{
i++;
}
file.close();
return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
// 初始化
string exePath = ReadLine("parameter.txt",1);
string command1 = ReadLine("parameter.txt",2);
string command2 = ReadLine("parameter.txt",3);
cout << "命令格参数:" <<endl;
cout << exePath << endl;
cout << command1 << endl;
cout << command2 << endl;
getchar();
return 0;
}
效果图: