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?

Recommended Answers

All 3 Replies

Use loops. Then use the iterator variable as variable that references the array. Eg

for (int i=0; i<50; i++) {
   fscanf(file, "%i", salary[i]);
   // ...
}

Of course, I'm assuming a lot of things here. If you're using C++ instead of C, you would be using ifstream and not fscanf(). And you would probably have more statements in the loop then just that one fscanf(). Also, you may be using a while() loop that keeps going until End Of File is reached. Then, you would need to use a counter variable to keep track of the iterations. But the idea is still the same.

Hope this helps

After 30 min of thinking , I came up with this:

#include <fstream>
#include <iostream>

using namespace std;

int main()

{
    int i,poz=0;
    int a[100]={0};

    fstream f("c:\\graf.txt",ios::in); 

    while(f>>i)    
        a[poz++]=i;

    for(i=0;i<poz;i++)
        cout<<a[i]<<" ";

    cout<<endl;


    return 0;
}

I dont have much experince with fstream functions...but i hope this will do the job
This is in C++ btw

Good luck

You'll need to write a parser for the file. For instance, if you read the first line "Year: 2005" you know the following set if or that year. The next line has has "Number of Employees: n" (let n be a number), so you know that you'll have that much data. The line after "Number of Employees" looks to be a header, so you can throw it out. Then you iterate n times to get your data, putting it into whatever data structure you deem worthy. At that point the next line will probably have another "Year: yyyy" (yyyy being whatever year it is) and you just repeat the process until either the end of the file or till the data is incorrectly formatted. I'm assuming the latter won't occur ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.