C++ c++;从包含特定元素的文件的一组元组中检索模式

C++ c++;从包含特定元素的文件的一组元组中检索模式,c++,file,tuples,C++,File,Tuples,我有一个文本文件,其中每一行都是以下形式的元组: 数字名称1名称2布尔值 其中“boolean”是一个bool变量,它是0或1 我想读取文件并显示包含相同数字的所有元组。 我有下面的代码,编译后没有给我任何错误,但在引入一个数字后它停止工作 #include <iostream> #include <fstream> #include <string> using namespace std; int main () { int a,e;

我有一个文本文件,其中每一行都是以下形式的元组:

数字名称1名称2布尔值

其中“boolean”是一个bool变量,它是0或1

我想读取文件并显示包含相同数字的所有元组。 我有下面的代码,编译后没有给我任何错误,但在引入一个数字后它停止工作

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{


    int a,e;
    string str1, str2, line;
    bool b;

  printf ("Enter pattern number: ");
  scanf ("%i",e);

  ifstream myfile ("entry.txt");
  if (myfile.is_open())
  {
    while ( myfile >> a >> str1 >> str2 >> b )
    {

        if (a==e)
        {

        cout << a << ' ';
        cout << str1 << ' ';
        cout << str2 << ' ';
        cout << b << '\n';

    myfile.close();
    }
  }
}

  else cout << "Unable to open file"; 

  return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int a,e;
字符串str1,str2,行;
布尔b;
printf(“输入图案编号:”);
scanf(“%i”,e);
ifstream myfile(“entry.txt”);
如果(myfile.is_open())
{
而(myfile>>a>>str1>>str2>>b)
{
如果(a==e)
{

cout您必须将变量的地址传递给
scanf()
,以便它可以修改变量。将
int
传递给期望
int*
的函数具有UB

因此,传递一个地址->使用引用(
&
)操作符

而且效果很好

或者简单地使用
std::cin
stream

std::cin >> e;

欢迎使用Stack Overflow!听起来您可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行程序,并查看它偏离预期的位置。如果您要进行任何编程,这是一个必不可少的工具。进一步阅读:
scanf(“%i”,e)<代码> >代码> SCANF(“%i”,e)和代码>也不要混合C和C++流处理。<代码> PrimTf(“输入模式编号:”);SCANF(“%i”,E);< /C> >可以是<代码> STD::CUT> E;< /CU> UB代表什么?
scanf ("%i",&e)
std::cin >> e;