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

  • SEARCH
  • Home
  • 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 8993975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:18:58+00:00 2026-06-15T23:18:58+00:00

In order not to forget C (at work I mostly have to deal with

  • 0

In order not to forget C (at work I mostly have to deal with XSLT and other languages that are much less fun), from time to time I like to write a small utility program.

An application that replaces strings in any kind of file without producing a second file (is there a nicer phrase that describes this?) is what came to my mind today.

What I have come up with is as follows:

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

#define CHUNKLEN 256

void replcnks(char *str, char *cnk1, char *cnk2);

int main(int argc, char **argv)
{
    FILE *file;
    char *old, *new, *fname, buf[CHUNKLEN];
    long len;

    if(argc != 4)
    {
        puts("Invalid arguments\n");
        return 0;
    }

    old = argv[1];
    new = argv[2];
    fname = argv[3];

    file = fopen(fname, "rb+");

    do
    {
        len = fread(buf, 1, CHUNKLEN, file);
        replcnks(buf, old, new);
        fseek(file, -len, SEEK_CUR);
        fwrite(buf, 1, len, file);
        fseek(file, len, SEEK_CUR);
    }
    while(len == CHUNKLEN);

    fclose(file);

    return 0;
}

void replcnks(char *str, char *cnk1, char *cnk2)
{
    char *pos;
    int clen1 = strlen(cnk1), clen2 = strlen(cnk2);
    while((pos = strstr(str, cnk1)))
    {
        memmove(pos + clen2, pos + clen1, strlen(pos) - clen1 + 1);
        memcpy(pos, cnk2, clen2);
    }
}

The program works, but it produces minor errors when the replacement string has a different size than the original one. When it is longer, the ending of the file is cut; when it is shorter, the output has a few garbage values in some places. I guess the problem lies in one of the fseeks.

How to fix this program?

The solution is probably straightforward, but I guess I’m out of brainpower today – but I would like to sleep peacefully :).

  • 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-06-15T23:19:00+00:00Added an answer on June 15, 2026 at 11:19 pm

    You can’t really do what you’re trying to do, as there’s no good way to replace a chunk of a file with a differently sized chunk.

    It also fails if the string being replaced falls across a chunk boundary.

    And it’ll probably do very bad things if replacing a short string with a longer one, as you don’t have room in the buffer for that.

    If you really really wanted to do this, you’d have to have some kind of buffering scheme where you track reading and writing separately, such that you don’t write bytes you haven’t read yet. And then you’ll still have to cope with growing or truncating the file…

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

Sidebar

Related Questions

Does the standard guarantee that order of equal elements will not change (eh, forgot
Problem: I want to list n number of games from each genre (order not
I want to change order in plist file not programmatically but from xcode. Is
I am using Autofac to handle dependency injection in my application. In order not
Here's the HTML I'm trying to use: <h1>Order Not Paid<span class=not-paid></span></h1> Of course if
Hi Order By not working on MySql the code is as follows, select *
According to the Puppet documentation: Order does not matter in a declarative language. If
I'm look to naturally sort an array, in reverse order and not preserve the
Order by descending is not working on LINQ to Entity In the following Query
Our saxparser does not ignore the byte order mark  which appears at 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.