| | |
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
![]() |
•
•
Join Date: Dec 2006
Posts: 3
Reputation:
Solved Threads: 0
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;
}
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;
}
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.
>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.
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
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*
•
•
Join Date: Jul 2005
Posts: 1,749
Reputation:
Solved Threads: 283
>>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.
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.
•
•
•
•
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
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? ![]() |
Similar Threads
- how to store and retrieve data from cookies (ASP.NET)
- opening file in world from server side (ASP.NET)
- Coding help - Is this possible? I think it is. (PHP)
- Need Help in Reading characters from a text file (C++)
- how to store the data? (Java)
- opcode and instructions in bits (C++)
- Perl/CGI (Saving Data) Part III (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: accessing a bmp file in C/C++
- Next Thread: Homework assistance
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






