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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:35:29+00:00 2026-06-03T20:35:29+00:00

I got some code and I want improve it to find and replace bytes

  • 0

I got some code and I want improve it to find and replace bytes in file
so
I want to find all origbytes in FILE and then replace it with newbytes
then save file, I know how to open, write and save, but hot I can find bytes in char?

 FILE* file;
 file = fopen("/Users/Awesome/Desktop/test", "r+b");
 int size = sizeof(file)+1;
 char bytes [size];
 fgets(bytes, size, file);
 for (int i=0; i<size; i++){ 
     char origbytes  []  = {0x00, 0x00};
     char newbytes   []  = {0x11, 0x11};
     if (strcmp(bytes[i], origbytes)) //Here the problem
     {
         fseek(file, i, SEEK_SET);
         fwrite(newbytes, sizeof(newbytes), 1, file);
     }
 }
 fclose(file);
  • 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-03T20:35:31+00:00Added an answer on June 3, 2026 at 8:35 pm

    strcmp() is for string compare and not character compare. Two characters can be compared directly

    if ( bytes[i] == origbytes[something] )
    

    Also you you should not apply sizeof() on a file pointer to determine file size. You should seek to the end of file using fseek and then query ftell except for binary files. For binary files, use something like fstat

    One more thing to note is that fgets returns much before the EOF if it sees a newline. Hence in your code, you may not read entire file contents even after doing the changes that we suggested. You need to use fread appropriately

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

Sidebar

Related Questions

I've got some code that runs all lovely on SQL Server 2008, however I'm
I've got some code like the following. I want it so that when I
Got some code which loads an input box, and then below this is an
I've got some C code that I want to expose to Python. It has
In order to improve type safety in some C library code, I got the
I've got some code I want to only execute on the latest iPhone SDK
I've got some code that performs some legacy 'database' operation and then processes the
I've got some very basic code which works, and I want to turn it
I've got some code dealing with HTTP requests and I want to unit-test it.
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)

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.