Hello Guys,
I've tried my best but it won't work.
I'm trying to read an integer from a txt file and return the whole line where this integer resides.
So, for example, in my txt file, if I have a line: aaa bbb ccc 100 bb 9
and I type in: 100
It'd return: bb 9
I want it to return the whole line.
Here's the compilable code:
#include <iostream>
#include <fstream>
using namespace std;
ifstream in;
ofstream out;
char a[255];
char b[255];
void search (char b[]) {
while (!in.eof())
{
in >> a;
if (strcmp(a,b)==0) {
in.getline(a,255);
cout << a <<endl;
}
}
}
int main()
{
in.open("test.txt");
if(in.fail()) {
cout << "Couldn't open!";
}
cin >> b;
search(b);
in.close();
return 0;
}
test.txt:
aaa bbb ccc 100 ddd 0
bbb aaa ccc 200 ddd 1
Thanks for your help!