Hi,

I wish to do the following things:

a) Append the first three lines from the file "file1.txt" at the end of another file "file2.txt"
b) Run a program "prog1.cpp" which uses the file file2.txt.
c) Delete the three lines appended at the end of the file "file2.txt". and append next three lines from the file "file1.txt" to file "file2.txt".
d) Run the program "prog1.cpp" which uses the changed file file2.txt".

I need to do the above steps again and again.

I do not how to implement it. Please guide me with how to proceed.

Thanks,

Dani AI

Generated

For : a robust, simple pattern is to (1) read three lines from file1, (2) append them to file2 while saving file2's original size, (3) run the program, then (4) truncate file2 back to its original size. Repeat until file1 is exhausted. This avoids trying to surgically delete "last three lines" (which is fiddly and error-prone) and works even when the final chunk has fewer than three lines.

Below is a compact C++17 example that shows the idea. It uses std::filesystem to read the original file size and to truncate the file after running the program. Adjust the program invocation (the string passed to std::system) to match your executable name and path.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <filesystem>
#include <cstdlib>

int main() {
    const std::string inPath = "file1.txt";
    const std::string outPath = "file2.txt";
    std::ifstream fin(inPath);
    if (!fin) { std::cerr << "cannot open " << inPath << '\n'; return 1; }

    while (true) {
        std::vector<std::string> chunk;
        std::string line;
        for (int i = 0; i < 3 && std::getline(fin, line); ++i) chunk.push_back(line);
        if (chunk.empty()) break;

        std::uintmax_t origSize = 0;
        if (std::filesystem::exists(outPath)) origSize = std::filesystem::file_size(outPath);

        {
            std::ofstream fout(outPath, std::ios::app);
            if (!fout) { std::cerr << "cannot open " << outPath << '\n'; return 1; }
            for (auto &s : chunk) fout << s << '\n';
        }

        int rc = std::system("prog1"); // waits for prog1 to finish
        if (rc != 0) std::cerr << "prog1 returned " << rc << '\n';

        std::filesystem::resize_file(outPath, origSize);
    }
    return 0;
}

Notes and cautions: std::system blocks until the program exits, so truncation happens after prog1 finishes; ensure prog1 closes file2 before it exits (otherwise truncation may fail on some platforms). If you prefer not to mutate file2, create a temporary file that contains file2+chunk and run prog1 on that copy (rename atomically if needed). For the truncation helper see std::filesystem::resize_file. As suggested, read up on basic file I/O if any steps are unfamiliar; post code if you hit a specific error.

Recommended Answers

All 4 Replies

.

When you hit the wall post your code and we'll help further.

yeah ... i'm just not seeing where the "Urgency" is here

i thought someone was bleeding on the floor.

commented: Indeed. +16

yeah.... if u cannot help i suggest do not post such remarks "i thought someone was bleeding on the floor.".

guest7, you've already made several unseemly blunders, but now you are risking being completely ignored.

Please read up before becoming belligerent.
How To Ask Questions The Smart Way

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.