Still need help with opening a file and store the data on it in an array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2006
Posts: 3
Reputation: Cnaly is an unknown quantity at this point 
Solved Threads: 0
Cnaly Cnaly is offline Offline
Newbie Poster

Still need help with opening a file and store the data on it in an array

 
0
  #1
Dec 12th, 2006
Could someone help me?

I have an file in which a list of employees and salaries are listed for each year

ex:

Year: 2005
No. of Employees: 3
Employee ID Salary
123456 36000.00
123567 32000.00
123678 33000.00
Year: 2006
No. of Employees: 4
Employee ID Salary
133456 31000.00
133567 32000.00
133678 33000.00

How do I open all these data into arrays?

This is my code, and I don't think it is good since it only reads the lines and not the data#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
usingnamespace std;
int main()
{
int counter;
string countLines[20];
ifstream indata;
indata.open("employee.txt");
for(counter=0;counter<=20;counter++)
{
getline(indata,countLines[counter]);
cout<<countLines[counter]<<endl;
}
indata.close();
return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,596
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1488
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Still need help with opening a file and store the data on it in an array

 
0
  #2
Dec 12th, 2006
wasn't the answers you received in the thread you created on 4 December sufficient? If not, you should just continue with that thread instead of creating a new thread with the same problem.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 3
Reputation: Cnaly is an unknown quantity at this point 
Solved Threads: 0
Cnaly Cnaly is offline Offline
Newbie Poster

Re: Still need help with opening a file and store the data on it in an array

 
0
  #3
Dec 12th, 2006
I think that you shouldn't be that mean. I am just trying to learn, and if whatever they wrote back to me doesn't work, I need to keep trying don't you think.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,823
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 748
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Still need help with opening a file and store the data on it in an array

 
0
  #4
Dec 12th, 2006
>I think that you shouldn't be that mean.
There was nothing mean about his post. It was politely worded as far as I can tell. The problem is that when you create a new thread for the same problem as your other thread, we basically lose all of the work that was done in the previous thread. This wastes your time as we have to start the troubleshooting process all over again to get a feel for your problem.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Still need help with opening a file and store the data on it in an array

 
0
  #5
Dec 12th, 2006
here is your old thread, as a reference. What did you try and what went wrong with it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Still need help with opening a file and store the data on it in an array

 
0
  #6
Dec 12th, 2006
No. of Employees: 4
Employee ID Salary
133456 31000.00  line 1
133567 32000.00  line 2
133678 33000.00  line 3

Why are there only three lines? Shouldn't there be four?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,749
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Still need help with opening a file and store the data on it in an array

 
2
  #7
Dec 12th, 2006
>>it only reads the lines

Thats what getline() does. It places all information in the input stream up to the indicated terminating character into the string indicated. If it the end of file indicator, EOF, before it finds a terminating char it will also cease input into the string indicated. Since the default deliminting value is the newline char, if there is no newline char in the file, then it will read in the whole file into a single string. In this case, there is probably a newline char at the end of each line. Therefore each line will be read into a "different" string. If you change the terminating char to be a colon char like this:

string temp;
getline(indata, temp, ':');

then you could read in the initial line of the file up to the colon after Year, remove the colon from the input stream, and just don't do anything with temp if you want. Then you could use a call to the >> operator to read the year into an int variable for later use if you want. You can break up (parse) the entire file into strings and numerical variables using getline() to read in strings and the >> operator to read in the numerical values, storing what you want in appropriate variables/arrays/whatever.

Be aware however that >> doesn't remove terminating whitespace char from the input stream. For instance it won't remove the newline char that's present in the file after 2006, even though you don't see a newline char when reading the file with your eyes. Therefore if you next call getline() with the default newline char as the terminating char the first thing getline() will find in the input stream is the newline char left behind by >> and it won't put anything useful in the string indicated. Then if you try reading 3 into a int variable the program will likely crash, because the string "No. of Employess:" is still in the input stream and you can't put a string into an int. To prevent this you should call the ignore() istream method after the call to >> if you are going to use a call to getline() next. Since you are reading in a file, it's unlikely there is more than one new line at the end of each line, though if there are any "empty" lines in the file there may be two newlines in succession. If there aren't any empty lines in the file, and your post suggests there isn't, then ignoring just a single char after the last consecutive call to >> should work. Otherwise you are going to need a more sophisticated syntax.

To avoid the hassle of throwing in calls to ignore() it is perfectly acceptable to read in the file line by line as one string per line using nothing but getline() as you originally did. However, then you would need to parse each string using a stringstream or a function that you create yourself thereby creating strings you can ignore and numerical variables you want to store.

It's probably just as easy to parse the file as you read it since it is predictable, but the choice is yours.

>>Why are there only three lines? Shouldn't there be four

Probably a typo and should be 3 instead of 4 just like the second number in the employee ID numbers are probably all 2s instead of 3s so that all three employees are represented in 2006 and 2005 rather than 3 different employees in each year, but who knows for sure.
Last edited by Lerner; Dec 12th, 2006 at 6:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 492
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Still need help with opening a file and store the data on it in an array

 
0
  #8
Dec 13th, 2006
Originally Posted by Cnaly View Post
Could someone help me?

I have an file in which a list of employees and salaries are listed for each year

ex:

Year: 2005
No. of Employees: 3
Employee ID Salary
123456 36000.00
123567 32000.00
123678 33000.00
Year: 2006
No. of Employees: 4
Employee ID Salary
133456 31000.00
133567 32000.00
133678 33000.00

How do I open all these data into arrays?

This is my code, and I don't think it is good since it only reads the lines and not the data
After you've read a line from the file as a string, you can parse the string to extract the data you need.

It would be helpful to define the rules as to the layout of your file (eg, first line is always a header containing the year, second line is always a header containing the number of employees, etc). At first glance its not immediately clear whether your file has any consistancy. If possible, you may want to change the format of your file somewhat (eg, by adding delimiting characters which show the start and end of the data, and even its type). Once you've clearly defined the format of your file, you can create functions which will parse the input to extract each item of useful data, and ignore superfluous information.

When you've defined your file format 'rules' - try creating a function which parses just the first line, and test it. If you can manage to do that, the rest of the file shouldn't be too different.

(Hint : When it comes to converting numbers to integral or floating point types, use std::stringstream)
Last edited by Bench; Dec 13th, 2006 at 7:24 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC