Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ visualc&x2B+;ifstream读取文件并分配给变量_C++_Ifstream - Fatal编程技术网

C++ visualc&x2B+;ifstream读取文件并分配给变量

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

我试图在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
     }