I try to fix it,but i can't Please help me!!

Dani AI

Generated

— the fastest, most reliable fix is to stop comparing characters manually and let the standard library do the line reading and searching. Your current char-by-char loops, the text[i] != '\n' check and the goto make the logic hard to reason about and are the source of the bug. As pointed out, avoid goto and system("pause"); they are unnecessary and can mask real problems.

Read the file line by line with std::getline into a std::string, then use std::string::find to test for the search token. This is simple and works for most substring searches:

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

int main() {
    std::ifstream infile("bufferfile.txt");
    if (!infile) { std::cerr << "Unable to open file\n"; return 1; }

    std::string search;
    std::cout << "Enter word to search: ";
    std::getline(std::cin, search);

    std::string line;
    while (std::getline(infile, line)) {
        if (line.find(search) != std::string::npos) {
            std::cout << line << '\n';
        }
    }
    return 0;
}

If you need case-insensitive matches, convert both the line and the search string to lowercase (use std::transform and std::tolower). If you need whole-word matches (so searching for "cat" does not match "concatenate"), use std::regex with word boundaries (compile with C++11 or later), but be careful to escape regex-special characters in the search term.

Troubleshooting tips: compile with warnings enabled (e.g., g++ -Wall -Wextra -std=c++17), verify the file path (use an absolute path or run the program from the file's directory), and test with a tiny file first. Converting your code to std::string + std::getline will make it readable, portable, and easy to extend.

Recommended Answers

All 3 Replies

Hi,I am Really new in C++. I'm trying to write a program which search word from text file and display matched word whole lines. Can any body help me out please?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{   int i;

    char word[30];
    char text[50];
    fstream dict("bufferfile.txt",ios::in);

    if (!dict.is_open())
    { 
        cout << "Unable to open file";
        system("pause");
        exit(1);
    }
    cout<<"Enter word to search \n";
    cin>>word;

    loop:
            i=0; 
    while(dict.getline(text,50))
    {
     while((word[i]!='\0')&&(text[i]!='\n'))
    { if(word[i] != text[i])
    {
              goto loop;}
                 else 

        cout<<text<<endl;


        }
}   ++i;
system("pause");
    return 0;
}

A list of things to do:
Use code tags, realize you're programming in C++ not BASIC avoid goto , avoid system() whenever possible(it's slow, often unportable, and insecure in some cases), and reformat your code because someplaces look like errors but I can't tell.

Yes it some Error occured but i don't know hoe to deal with it.
If you know please help me,,thank you so much

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.