File handling

Hey guys am new to c++...so am trying to write a program in c++ that will read from two files...i need it to read from the first file and store data in variables firstName,lastName and Registration code,the using the registration code it checks or search in the second fiile if the registration code exists in the second file..
Any help will be greatly appreciated
Last edited on
As a start, check out the tutorial: http://www.cplusplus.com/doc/tutorial/files/
Another tutorial: https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm

Try to write some code yourself, and once you get stuck, post what you've tried, and we can help you from there. Also, show how the file are formatted.
Thanks Ganado for the links
here is what i have tried to do

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Applicant

{
public:

string getA();
string getB();
int getC();

private:

string A;
string B;
int C;

void GetInformation
{
firstfile >> A >> B >> C;
cout << "First Name: " << A << endl;
cout << "Surname: " << B << endl;
cout << "Registration Code: " << C << endl;

}

};



int main()
{
ifstream firstfile,secondfile;
firstfile.open("From Form.txt");
second.open("StoreDatabase.txt");



string A;
string B;
int C;


firstfile >> A >> B >> C;
cout << "First Name: " << A << endl;
cout << "Surname: " << B << endl;
cout << "Registration Code: " << C << endl;


//this is where am stuck the most!!

//Function to search in the Registration Code in the StoreDatabase
//creates and wirtes to a new file named confirmed customters

return 0;
}
Let's assume that the secondFile has the same format as firstFile.
1
2
3
4
5
6
7
8
9
10
11
string first_name, last_name;
int reg_code;
second.open("StoreDatabase.txt");
if (second)
{
  while (second >> first_name >> last_name >> reg_code)
  {
     // now compare the input data with the data just read
     // if the are equal save them to a new file named confirmed customters
  }
}
Thanks Thomas!
So if i were to write that in my code like below....

"From my code"

firstfile >> A >> B >> C;
cout << "First Name: " << A << endl;
cout << "Surname: " << B << endl;
cout << "Registration Code: " << C << endl;

then i write in my code like so

firstfile >> A >> B >> C;
cout << "First Name: " << A << endl;
cout << "Surname: " << B << endl;
cout << "Registration Code: " << C << endl

string first_name, last_name;
int reg_code;
second.open("StoreDatabase.txt");
if (second)
{
while (second >> first_name >> last_name >> reg_code)
{
// now compare the input data with the data just read
// if the are equal save them to a new file named confirmed customters
}
}
Last edited on
While waiting any response i took time to try this.............


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
86
87
88
89
90
91
92
93
94

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Applicant

{
public:

string getA();
string getB();
int getC();

private:

string A;
string B;
int C;
};



int main()
{
ifstream firstfile,secondfile;
firstfile.open("From Form.txt");
secondfile.open("StoreDatabase.txt");


string A;
string B;
int C;


firstfile >> A >> B >> C;
cout << "First Name: " << A << endl;
cout << "Surname: " << B << endl;
cout << "Registration Code: " << C << endl;

//from here am trying to search for registration code "C"

int offset;
string line;
secondfile.open("StoreDatabase.txt");
cin >> C;

if (StoreDatabase.is_open()) //From here downwards am having errors :( 
  {
   while (StoreDatabase.eof())
     {
      getline(infile2,line);
        if ((offet = line.find(C, 0)) != string::npos)
           {
               cout << "Found!" << C << endl;
            }

            fstream Confirmed;
           Confirmed.open("Confirmed_customers.txt",ios::out);
           if(!Confirmed_customers)
          {
         cout<<"Confirmed customers File creation failed";
         }
        else
        {
         cout<<"Confirmed_customers file successfuly created!";
         Confirmed<<"<< A << B << C <<";
         Confirmed.close();

        }
       infile2.close();
 }
 else
 cout <<"Could not find" << C <<endl;

fstream NotConfirmed;
NotConfirmed.open("NotConfirmed/Non customers.txt",ios::out);

if(!NotConfirmed)
{
cout<<"Non Customers File creation failed";
}
else
{
cout<<"Non Customers file Created!";

NotConfirmed<<"<< A << B << C <<";

NotConfirmed.close();

return 0;
}


Topic archived. No new replies allowed.