Searching thru files

Hi,

Would you please help me... I have below code and it only search the first line.. I think I am missing something or my code is wrong...

My goal is to have a specific category be search... if search, I would like to modify it... a guide will do... thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
void search (){
	ifstream in_File ("Student.txt");
	fstream out_File ("Temp.txt", ios::in | ios::out);
	if(!in_File.is_open()){
		cout << "File failed to open" << endl;
	}	

	string strSN;
	string strAge;
	string line;
	
	system ("cls");
	int count = 0;
	while (getline(in_File, line)){
		stringstream ss (line);
		stringstream strGender;
		stringstream strCN;
		stringstream stremail_add;
		stringstream strProg;
		getline(ss, strSN, ',');
		stu.student_number = atoi(strSN.c_str());
		getline(ss, stu.first_name, ',');
		getline(ss, stu.middle_name, ',');
		getline(ss, stu.last_name, ',');
		strGender << stu.gender;
		strGender >> stu.strGender1;
		getline(ss, stu.strGender1, ',');
		getline(ss, strAge, ',');
		stu.age = atoi(strAge.c_str());
		stu.student_number = atoi(strSN.c_str());
		getline(ss, stu.birth, ',');
		getline(ss, stu.address, ',');
		strCN << stu.contact_number;
		strCN >> stu.strCN1;
		getline(ss, stu.strCN1, ',');
		stremail_add << stu.email_add;
		stremail_add >> stu.stremail_add1;
		getline(ss, stu.stremail_add1, ',');
		getline(ss, stu.father_name, ',');
		getline(ss, stu.mother_name, ',');
		strProg << stu.program;
		strProg >> stu.strProgram;
		getline(ss, stu.strProgram, ','); 
		out_File << "==================================================================================================" << endl << endl
				<< stu.student_number << endl
				<< stu.first_name << endl
		 		<< stu.middle_name << endl
				<< stu.last_name << endl
				<< stu.strGender1 << endl
				<< stu.age << endl
				<< stu.birth << endl
				<< stu.address << endl
				<< stu.strCN1 << endl
				<< stu.stremail_add1 << endl
				<< stu.father_name << endl
				<< stu.mother_name << endl
				<< stu.strProgram << endl;
						int key;

			while(!out_File.eof()){
						cout << "Enter SN ";
		cin>> key;
		if(key == stu.student_number){
		cout << "==================================================================================================" << endl << endl
				<< stu.student_number << endl
				<< stu.first_name << endl
		 		<< stu.middle_name << endl
				<< stu.last_name << endl
				<< stu.strGender1 << endl
				<< stu.age << endl
				<< stu.birth << endl
				<< stu.address << endl
				<< stu.strCN1 << endl
				<< stu.stremail_add1 << endl
				<< stu.father_name << endl
				<< stu.mother_name << endl
				<< stu.strProgram << endl;
			}
	}

		in_File.close();


	}
		}
Your poor indentation has blinded you to the obvious
1
2
3
4
while (getline(in_File, line)) {
    // snipped
    in_File.close();
}

You close the file after just one line!

> while (!out_File.eof())
Since you don't touch out_File inside this loop, I have to wonder what your plan is here.
Thanks Salem. I would like to search the file by account number and get the whole of this... but when I tried to search it only get the first line. Would you be able to help me on that please. Thanks

<< stu.student_number << endl
<< stu.first_name << endl
<< stu.middle_name << endl
<< stu.last_name << endl
<< stu.strGender1 << endl
<< stu.age << endl
<< stu.birth << endl
<< stu.address << endl
<< stu.strCN1 << endl
<< stu.stremail_add1 << endl
<< stu.father_name << endl
<< stu.mother_name << endl
<< stu.strProgram << endl;
Are you trying to copy all the records from "Student.txt" to "Temp.txt", where this is true
> if(key == stu.student_number)
yes, salem.. after copying into the file ("Student.txt" to "Temp.txt") it will search the inputted student number (Key) and if it is the same with the stu.student_number.. it will output in C++ with all the given categories...

Thanks!!
Well searching through Temp.txt is going to be a right royal PITA because the information you want to filter out is spread out over many lines.

Filtering while you read will be so much easier
1
2
3
4
5
6
7
8
9
cout << "Enter SN ";
cin>> key;
while (getline(in_File, line)) {
    parseLine(line,stu);  // yes, make that mass of lines a separate function.
    if(key == stu.student_number) {
        out_File << "=====" << endl << endl
                 << stu.student_number << endl
    }
}




What's going on here?
1
2
3
		strGender << stu.gender;
		strGender >> stu.strGender1;
		getline(ss, stu.strGender1, ',');

You haven't read anything into stu.gender yet.
Topic archived. No new replies allowed.