sadada 0 Newbie Poster
Hey,

I crated a program which can search a text for a string and gives you every line which conatins that string back.
Now I want to create another text file with the output. But I cant get it to work. Actually I have no problems with creating the text file becasue its creating it but its always empty. I just dont know how to gte the results from the top of my code into the output text file.

Here is my code:

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

using namespace std;

int main(void) {
    string search;
    int offset;
    string line;

    ifstream MyFile;
    MyFile.open("Text.txt");
    cout << "Type the name you want to search" << endl;
    cin >> search;

    if (MyFile.is_open())
    {
        while (!MyFile.eof())
        {
            getline(MyFile, line);
            if ((offset = line.find(search, 0)) != string::npos) {
                cout << "The word was found " << line << endl;
            }

        }

        string filename;
        stringstream str;
        cout << "Name your new text file";
        cin >> filename;
        filename += "_OP.txt";
        cout << filename;
        ofstream (filename.c_str());

        MyFile.close();
    }
    else
        cout << "Wasnt able to open the file!" << endl;
    system("PAUSE");
    return 0;
}

Dani AI

Generated

— The reason the new file is empty is not that the file isn't created, but that nothing is written to it. In your program the matched lines are only sent to cout, and the output stream you try to create is not stored in a variable (so it is constructed and immediately destroyed). Also, prefer while (getline(...)) instead of while (!eof()) and use getline(cin, ...) for multi-word searches.

Here is a compact, corrected example you can drop into your program:

#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream in("Text.txt");
    if (!in) { std::cerr << "Cannot open input file\n"; return 1; }

    std::string query;
    std::cout << "Search for: ";
    std::getline(std::cin, query);

    std::string outname;
    std::cout << "Output file name (no extension): ";
    std::getline(std::cin, outname);
    if (outname.empty()) outname = "results";
    outname += "_OP.txt";

    std::ofstream out(outname);
    if (!out) { std::cerr << "Cannot open output file\n"; return 1; }

    std::string line;
    while (std::getline(in, line)) {
        if (line.find(query) != std::string::npos) {
            std::cout << line << '\n';
            out << line << '\n';    // write the matched line to the file
        }
    }
    return 0;
}

Notes and troubleshooting

  • Always check in and out after opening.
  • while (getline(in, line)) avoids EOF pitfalls.
  • Use std::size_t pos = line.find(query) and compare to string::npos if you need the index.
  • If you want to append instead of overwrite, open with std::ofstream out(outname, std::ios::app).
  • If the file still seems empty, verify the program's working directory (the file may be created elsewhere).

This addresses the two root causes: no writes to the file, and creating an unnamed temporary ofstream instead of keeping one to write into.

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.