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 982663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:44:00+00:00 2026-05-16T04:44:00+00:00

The Problem was to find and replace a string in a C File .

  • 0

The Problem was to find and replace a string in a C File.

I am new to C Files. I have tried the following code but I didnt get any output:

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        FILE *f1,*f2;
        char *src,*dest,*s1,ch,ch1,ch2,ch3;
        int i;
        f1=fopen("input.txt","rw");
        f2=fopen("dummy.txt","rw");
        src="mor";
        dest="even";
        while(ch!=EOF)
        {
         ch=fgetc(f1);
         if(ch==src[0])                      //Finding 1st char of src
         {
         fgets(s1,strlen(src),f1);
         if(strcmp(src+1,s1)==0)         //Finding occurance of "src" in file
         {
          fseek(f1,strlen(src)-1,SEEK_CUR);
          while(ch1!=EOF)             //Copying remaining data into another file
          {
          ch1=fgetc(f1);
          fputc(ch1,f2);
          }
      fseek(f1,-strlen(src),SEEK_CUR);
      for(i=0;i<strlen(dest);i++)  //replacing "src" with "dest"
      {
          ch2=dest[i];
          fputc(ch2,f1);
      }
      fclose(f1);
      f1=fopen("input.txt","a");
      while(ch3!=EOF)      //Appending previosly copied data into 1st file
      {
          ch3=fgetc(f2);
          fputc(ch3,f1);
      }
     }
   }
 }
     fclose(f1);
     fclose(f2);
}

The Contents of input.txt is “morning”.

Kindly point the ERROR in the logic and also give an efficient code for the same.

Thanks in Advance.

  • 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-16T04:44:01+00:00Added an answer on May 16, 2026 at 4:44 am

    Reading files in C is usually a bit messy. The first problem that I see is the way ch is used in the main loop. The first time

    while (ch != EOF)
    

    is executed, ch is uninitialized, and if it happens to hold EOF, the main loop will not execute at all. I usually use the following structure for reading from files:

    FILE *fInput = fopen("input.txt", "r");
    int ch; /* need an int to hold EOF */
    
    for (;;)
    {
        ch = fgetc(fInput);
        if (ch == EOF) break;
    
        ...
    }
    

    In addition, you may need to read up on file pointer concept. For example, after reading the remainder of src, you fseek() forward, and skip some more characters before you copy data to f2. Essentially, you read m, read or (with fgets() – and into an unallocated buffer s1 that would go ka-boom on you some time in the near future), skip 2 more characters (now your pointer is at last n of “morning”), copy “ng” into f2, try to write EOF to f2 in this loop (hence the above pattern for reading until EOF), seek two characters back (which may fail once you reach EOF, my C file functions are a bit rusty these days), write “even” to f1 (which should, if I am wrong about seek after EOF, set input file to “mornieven”, and not change it if I am correct). In summary, I don’t think the code does what you intend it to do.

    I would recommend building up your function. Each one of the following can be written as a program that you should test and finish before going to next step:

    1. read the file safely, and print it out
    2. detect the contents of src, and print the rest of input
    3. save the rest of the input to second file instead of printing
    4. replace src with dest in first file, and ignore the rest (since you open input file with ‘rw’, this will truncate the rest of input). You may need to do an fseek() to clear the EOF status. Also look at ftell() to record a position that you can jump back to using fseek()
    5. finally, copy in everything you have saved to second file after replacing src with dest (no need to close f1 here. But it is better to open f2 as write, close after copy from first file, and reopen as read to perform the copy back to f1).

    In addition, when you need a buffer (such as s1), just use a large enough array for now, but look into malloc() and free() functions to perform dynamic memory allocations for situations like these.

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

Sidebar

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.