C++ visualc&x2B+;ifstream读取文件并分配给变量
我试图在vc++中通过ifstream读取输入文件。文件格式如下(仅2列,大文件): 我正在尝试读取text1和text2,如果我发现text2等于从用户输入中获取的字符串变量的值,那么我将为text1分配一个变量并执行一些操作 谁能给我一些关于如何编写代码的提示吗?我一直尝试用C++代码来与Visual C++集成,但是我有很多问题,包括从STD::string到Stry.< /P>转换。 我可以用shell脚本轻松地完成这项工作,但我想学习如何用vc++完成这项工作 我很新的Visual C++,请原谅我缺乏知识。C++ visualc&x2B+;ifstream读取文件并分配给变量,c++,ifstream,C++,Ifstream,我试图在vc++中通过ifstream读取输入文件。文件格式如下(仅2列,大文件): 我正在尝试读取text1和text2,如果我发现text2等于从用户输入中获取的字符串变量的值,那么我将为text1分配一个变量并执行一些操作 谁能给我一些关于如何编写代码的提示吗?我一直尝试用C++代码来与Visual C++集成,但是我有很多问题,包括从STD::string到Stry.< /P>转换。 我可以用shell脚本轻松地完成这项工作,但我想学习如何用vc++完成这项工作 我很新的Visual C
非常感谢您。您可以使用
sstream
和istringstream
轻松地将字符串从文件存储到任何字符串变量。我认为这段代码可能会有所帮助
string string1,ch;
ifstream file("filename");
while(getline(file,ch); //true while ch not equal to end of file.
{
istringstream ss(ch); //will read lines and stores in ss.
ss>>string1; //copy read string in string1
}
您需要
#包含才能执行上述操作。向我们展示您拥有的任何代码,我们将从那里开始。谢谢,但这样我就无法将我的System::String类型的变量与std:String进行比较,例如在“if”语句中的if(var1==var2){”。如何使用字符串^读取文件和存储变量?您是否在Microsoft developer network中读取了字符串。如果未读取,请读取链接
string string1,ch;
ifstream file("filename");
while(getline(file,ch); //true while ch not equal to end of file.
{
istringstream ss(ch); //will read lines and stores in ss.
ss>>string1; //copy read string in string1
}