Solved

How do I write a C++ program (coursework)

Posted on 2011-09-28
20
145 Views
Last Modified: 2015-06-23
How do I write a C++ program to compare two delimited text files, and apply the new entries and the changes to the main file

I have to write this program in C++ or VB which requires the following:

1.There are two delimited text files, both have the same format, one is the master file, the other is weekly file
2. Sample will be:
ID|firstname|middleInitial|lastName|Address1|Address2|City|State|Zip|Zip4|SSN#
2345|Andy|L|FEDELI|123 Street|Apt3|Springtown|FL|35107|1234|393381124

3.So, both files are in the same format
4.The program will check each of the lines by the ID, if it finds a match, it has to update the whole line to the master text file
5. If there is no ID match, add the whole line to the master file, which means we have a new customer added to the master file
0
Comment
Question by:soniaN310
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +3
20 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 36814358
You would do well to import this data into a database and do the updates with SQL.

This question is currently in the C++ Builder zone.  You said that you could also use VB.  What program development tools are at your disposal?
0
 

Author Comment

by:soniaN310
ID: 36818029
Microsoft Visual Studio 2005
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 36818487
Is this a homework assignment?
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 34

Expert Comment

by:sarabande
ID: 36889938
soniaN310, do you have any programming experience with either c++ or vb?

if yes, the task should be done with the language you like better.

in c++ you would create a structure (struct or class) which has all attributes as members (string or int members). then read master file record by record, parse each record and add it to a map using the id for key. then you could read the weekly file same way and merge the records from that into the map, which finally could be written back to a file what gives the new masterfile.

Sara
0
 

Author Comment

by:soniaN310
ID: 36892527
sarabande,
Thank you for your reply,
I don't have much experience with C++, just taking a C++ class for the first time.
Would you be able to help me with some code please?
Thank you,
soniaN310
0
 
LVL 34

Expert Comment

by:sarabande
ID: 36909000
i could only help with code when you firstly posted some own attempts to solve the question.

so, i would suggest you create a new c++ project  (win32 - console) and try to specifiy the structure i mentioned in my previous post. it is like

struct Address
{
// add here the members according to the telegram
};

Open in new window


then, add a main function and try to add code following the brief "design" i made.

when you need help, post your current work and ask.

Sara
0
 

Author Comment

by:soniaN310
ID: 36919066
Please help me from here



#include <iostream>
#include <fstream> 
#include <string>
#include <sstream>
#include<iomanip>
#include <algorithm>
#include <vector>
#include <iterator> 
#include <limits>


using namespace std;


 int main () 
{

	int customer_id;
                     string line; 
                     string customer_Fname, customer_Mname, customer_Lname, customer_Address1, customer_Address2, customer_City, customer_State, customer_Zip, Zip4, customer_SSN;

   
	ifstream newData("test.txt");
	vector<string>data1;
	if (newData.is_open())
	{
		string line;
		while(getline(newData, line))
		{
			data1.push_back (line);
		}
	}
	else
	{		
		cout << "Unable to open file test.txt";
		return 1;	
	}

    ifstream storeData("master.txt");
	if (storeData.is_open())
	{
		string line;
		while (getline(storeData, line))
		{
			for(size_t i = 0; i < data1.size(); ++i)
			{				
				if (line == data1[i])					
					data1.erase(data1.begin()+i);
			}
		}
	}
	else
	{
		cout << "Unable to open file master.txt";		
		return 1;
	}



    ofstream resultData("result.txt");
	if (resultData.is_open())
	{
		for (size_t i = 0; i < data1.size(); ++i)
		{			
			resultData << data1[i] << endl;		
		}	
	}	
	else
	{		
		cout << "Unable to open file result.txt";		
		return 1;	
	}	
	return 0;

Open in new window

0
 
LVL 23

Accepted Solution

by:
Michael74 earned 250 total points
ID: 36922427
To solve this I would

1/ Read the contents of the master file into a map. This would be very slow with large files but for this example would work fine. Use the ID as the key and the entire row as the value

Here is some info on C++ maps
http://www.cplusplus.com/reference/stl/map/

2/ The iterate through the weekly file and for each line
   - Extract the ID
   - Use the map find function to see if the ID exists.
      - If ID exists replace the value for that key with the contents of the line
      - If the ID does not exist then append line and key to the map

3/ Once completed overwrite the master file with the contents of the map by iterating through the map and adding the contents of each value as a line of the file. Remember that each value already contains the new line character

As noted this may not be the most efficient method but for the purpose of the assignment will work fine

Michael

0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 36922580
do you know the either file is sortesd by id?

If so all youve got to do is iterate down both files at the same time reading a line at a time.  check the updates line for id number (string handling), read down the main file until you either hit the update id or greater writing each line out to a temp file.  if you hit the same number or dont find it then you write that update line to the master file and carry on to the next update line.

If not in order then consider sorting them, bring them into a databse structure or recording the ids from update file in an array.  read each line of master file, if id matches one in array then replace with that and mark it used / remove from array.  if anything left at end from updates add them on the end.

Sorry that was a bit rambling in the end... Was clear in my head when i started writing it!
0
 
LVL 34

Expert Comment

by:sarabande
ID: 36922694
to continue with your approach first switch reading of newdata and master. that way you could add only those records to the vector data1 which were new.

then write code to extract the id from a record.

it is to find the position of the first separator with line.find('|') and then assign the substring from 0 to that position - what is line.substr(0, pos) - to a new string.

for the master.txt push the id to a new vector of strings.

for the newdata.txt also extract the id from line but then search the id in the id vector. you could do that by using std::find but for simplicity i would suggest to start a new loop and simply compare the id with all elements of the id vector. if you find the id you need to replace the record of same index in data1 vector with the new line from newdata. if the id doesn't match you simply add the new line to the data1 by push_back and also add the new id to the id vector. the latter would also prevent you from copying duplicate ids which were in the newdata.txt.

the final storing then would store the merged data to a new file.

Sara
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36922730
reason i  suggested reading the updates is I would assume you may  have, say, 10k customer records but only 50 are updated say..... But I guess it doesnt make a lot of difference either way unless the record sets are huge
0
 
LVL 34

Expert Comment

by:sarabande
ID: 36922772
to get rid of the tabs in your code and were using visual studio, you could select all text (ctrl+a)  and choose menu edit - advanced... there you find option to remove tabs from selection.

to permanently get spaces in your code instead of tabs you may change the settings of the editor(s) below tools - option.

Sara
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36947960
Don't forget to accept answer(s) then!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question