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?

Dani AI

Generated

Combine the ideas already posted: read the file with fstream, scan line-by-line, and treat each "Year:" block as a small record. Store results in a vector of year-records (each record contains the year and a vector of employee entries). Keep employee ID as a string (preserves leading zeros) and salary as a double. Use std::getline + std::istringstream to parse each meaningful line — that handles extra spaces and lets you skip the header line ("Employee ID Salary") cleanly. This follows 's loop idea, uses 's fstream approach, and implements 's parser suggestion in a robust way.

A minimal, practical parser (sketch) — reads "Year:", then "No. of Employees:", skips the header, then reads N employee lines:

#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iostream>

struct Employee { std::string id; double salary; };
struct YearBlock { int year; std::vector<Employee> employees; };

int main() {
    std::ifstream in("data.txt");
    std::string line;
    std::vector<YearBlock> data;

    while (std::getline(in, line)) {
        if (line.find("Year:") == 0) {
            YearBlock block;
            block.year = std::stoi(line.substr(line.find(':')+1));
            std::getline(in, line); // "No. of Employees: N"
            int n = std::stoi(line.substr(line.find(':')+1));
            std::getline(in, line); // skip header
            for (int i=0;i<n && std::getline(in, line); ++i) {
                std::istringstream iss(line);
                Employee e; iss >> e.id >> e.salary;
                block.employees.push_back(e);
            }
            data.push_back(std::move(block));
        }
    }

    for (auto &b : data) {
        std::cout << b.year << " : " << b.employees.size() << " employees\n";
    }
}

Troubleshooting notes: trim a trailing '\r' if the file came from Windows (remove it from line), guard stoi/stod with try/catch for malformed numbers, and validate that the actual number of read lines matches the declared employee count. For more complex formats consider a simple regex or switching to a structured format (CSV/JSON) for long-term maintainability.

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.