0
点赞
收藏
分享

微信扫一扫

C++ 读取 txt 文件件中某一行

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;
}

效果图:

C++ 读取 txt 文件件中某一行_初始化



举报

相关推荐

0 条评论