Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4121696
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:23:06+00:00 2026-05-20T23:23:06+00:00

hi every body can some one help me with thi problem i have a

  • 0

hi every body
can some one help me with thi problem
i have a text file which is following

hi Hello this is my Hello to
 the Hello world

i write a code in c which is following

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void main()
{
    FILE *fp,*fout;
    int i=0,len_string;
    char SearchText[]="Hello"; /* You can replace this text */
    char ReplaceText[]="Help"; /*You can also replace this text. */
    char temp[30];
    fp=fopen("ram.txt","a+");
    fout=fopen("temp.txt","a+");
    rewind(fp); /* for going to start of file. */
    if(fp==NULL || fout==NULL)
    {
        printf("File couldn't be opened ");
        exit(0);
    }
    len_string=strlen(SearchText);
    while(!feof(fp))
    {
        for(i=0;i<len_string;i++)
        { 
            temp[i]=fgetc(fp);
        }
        temp[i]='\0';
        if(strcmp(SearchText,temp)==0) /* the stricmp() is used for comparing both string. */
        {

            fprintf(fp,"%s ",ReplaceText);
            fprintf(fout,"%s",ReplaceText);
            fflush(fp);
            fclose(fp);
            fclose(fout);
            exit(1);
        }
    }
    fclose(fp);
    fclose(fout);
}

now my out put is like that

hi Hello this is my Hello to
 the Hello world

Help Help
what i m doing wrong ?
how to replace Hello to help in my text file ?
how to get my output like that?

hi Help this is my Hello to
 the Help world

can anybody explain with code ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:23:06+00:00Added an answer on May 20, 2026 at 11:23 pm

    You are searching in 5-char groups for the string “Hello”. So you are looking at your file like this:

    hi He
    llo t
    his i
    s my 
    Hello
     to\n 
    the H
    ello 
    world
    

    Only one of those matches what you are looking for. You should probably read the file line-by-line (using a larger buffer, just in case), and search/replace on each line for your search text.

    Additionally, you are exiting 0 on failure and 1 on success (which is backwards; 0 means success, anything else means failure, traditionally). And you are exiting after the first match, rather than continuing onward looking for more.

    To read line by line and perform search/replace, do something like this:

    FILE *f = ...;
    char buf[1024];
    while (fgets(buf, sizeof(buf), f)) {
        char *pos;
        while ((pos = strstr(buf, search)) != NULL) {
            char temp = *pos;
            *pos = 0;
            fprintf(out, "%s%s", buf, replace);
            *pos = temp;
            buf = pos + strlen(search);
        }
        fprintf(out, "%s", buf);
    }
    

    This isn’t ideal, as a long line (> 1023 chars) will get cut into pieces, possibly in the middle of a search token, but it will work in most cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

everybody; I have this problem in asp.net, I have a page where I insert
Every time I turn on my company-owned development machine, I have to kill 10+
Every time I create a new project I copy the last project's ant file
It seems that everybody knows you're supposed to have a clear distinction between the
Ok maybe everybody knows how to do this, but I've never try it beacause
So I purchased myself an iPhone ... everybody has one ... so I figured
Everybody knows that you should close a connection immediately after you finish using it.
Edit: I apologize everybody. I used the term jagged array when I actually meant
When testing in any language, how does everybody phrase their assertion messages ? I
I just started creating my data-access layer using LinqToSql. Everybody is talking about the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.