Hello everyone..I am planning to develop a payroll system in C++ using textfile as its database but i don't have any idea on how to begin it..Can any one help me with the codes please..How to view data into textfile, how to write data into textfile, how to save data into textfile..Thanks a lot guys.

Dani AI

Generated

As asked, a simple payroll on disk is best done by picking a stable record format, using fstream for I/O, and having small helpers for add/read/update/delete. pointed to online references and was right that file streams are the right tool. Below is a compact, practical approach and things to watch for in Visual C++.

Example (pipe-delimited text, keep names simple or escape delimiters):

#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <stdexcept>
#include <iomanip>

struct Employee { int id; std::string name; double salary; };

void appendEmployee(const Employee& e, const std::string& file) {
    std::ofstream ofs(file, std::ios::app);
    if (!ofs) throw std::runtime_error("open failed");
    ofs << e.id << '|' << e.name << '|' << std::fixed << std::setprecision(2) << e.salary << '\n';
}

std::vector<Employee> readAll(const std::string& file) {
    std::vector<Employee> out;
    std::ifstream ifs(file);
    if (!ifs) return out;
    std::string line;
    while (std::getline(ifs, line)) {
        std::istringstream ss(line);
        std::string sId, sName, sSal;
        if (std::getline(ss, sId, '|') && std::getline(ss, sName, '|') && std::getline(ss, sSal)) {
            out.push_back({std::stoi(sId), sName, std::stod(sSal)});
        }
    }
    return out;
}

Notes and tips: use ios::app to append, ios::trunc when overwriting. For update/delete read all into a vector, change it, write to a temporary file, close it and then std::rename over the original (avoids partial writes). Validate stoi/stod calls or catch exceptions to handle corrupt lines. Choose a delimiter that won't appear in names or implement quoting/escaping if you need commas.

Troubleshooting: if files "disappear" in Visual C++ check the program working directory (debugging settings) or use absolute paths. For multi-user access or atomic transactions, use a real database (SQLite) rather than raw text. For fstream and parsing details see the official refs: <fstream> header and <sstream> header.

Recommended Answers

All 2 Replies

You can always write data into text files using file streams. Check the Internet on file streams or go to http://www.codeguru.com for more info.

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.