how to format data in a file
Jun 2, 2020 at 11:51am UTC
Hello guys am working on a c++ project that search for Student id in the source file,if a match is found
it writes to the output file
after the search those were the found
1 2
John Bill M 1895865 20013647 2001 78 66 80 66 79 75
Frank Libmen M 6546727 20016367 2001 50 78 56 43 33 57
My code
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>
#include <sstream>
using namespace std;
int main()
{
struct person
{
string firstname;
string surname;
string gender;
int id;
};
system("cls" );
ifstream input;
ifstream source;
ofstream output;
ofstream notfound;
input.open("input.txt" );
source.open("source.txt" );
output.open("Output Processed file.txt" );
notfound.open("Not Found.txt" );
if (!input)
{
cout << "Error!" << endl;
cout << "Unable to access the input.txt file required to run this process..." << endl;
cout << "********************************************************************" << endl;
cout << "The Process Was not Sucessful!! enter 0 Return to Previous Menu" << endl;
}
if (source.fail())
{
cout << "Error!" << endl;
cout << "Unable to access the source.txt file required to run this process" << endl;
cout << "Its either it was Deleted or placed in a location inaccessable for this Program" << endl;
cout << "Please check and re-run the process" << endl;
cout << "" << endl;
cout << "********************************************************************" << endl;
cout << "The Process Was not Sucessful!! enter 0 Return to Previous Menu" << endl;
}
// foreach line of input....
string input_line;
while ( getline(input,input_line) )
{
stringstream i_line(input_line);
person input_person;
i_line >> input_person.firstname
>> input_person.surname
>> input_person.gender
>> input_person.id;
// search the source for a matching name
source.clear();
source.seekg(0);
bool found = false ;
string source_line;
while ( !found && getline(source,source_line) )
{
stringstream s_line(source_line);
person source_person;
s_line >> source_person.firstname
>> source_person.surname
>> source_person.gender
>> source_person.id;
// found them!
if ( source_person.id == input_person.id)
{
found = true ;
}
}
// and output...
if ( !found )
{
notfound << "No NRC match for:" << source_line << endl;
}
else
{
cout << "NRC Match Found for:" << source_line<< endl;
output << source_line<< endl;
}
}
input.close();
source.close();
output.close();
notfound.close();
cout << "" << endl;
cout << "********************************************************************" << endl;
cout << "The Process Was Sucessful!! enter 0 Return to Previous Menu" << endl;
}
output file expected format
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
First Name: John
Last Name: Bill
Gender: M
Student ID: 1895865
Course Code: 20013647
Year: 2001
Mathematics 78
English 66
Science 80
Geography 66
Music 79
Art 75
First Name: Frank
Last Name: Libmen
Gender: M
Student ID: 6546727
Course Code:20016367
Year: 2001
Mathematics 50
English 78
Science 56
Geography 43
Music 33
Art 57
Jun 2, 2020 at 12:40pm UTC
Topic archived. No new replies allowed.