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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:31:17+00:00 2026-06-01T20:31:17+00:00

I need to replace the a character in a text file with ‘?’. It’s

  • 0

I need to replace the a character in a text file with ‘?’. It’s not working as expected.

The file has contents ‘abc’ (without quotes) and i’ve got to use the unix system calls: lseek(), open() and write(). I can’t use the standard C file I/O functions.

The plan is to eventually exand this into a more generalised “find and replace” utility.

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(){

int file = open("data", O_RDWR); //open file with contents 'abc'
lseek(file,0,0); //positions at first char at beginnging of file. 
char buffer;
read(file,&buffer, sizeof(buffer));
printf("%c\n", buffer); // text file containing 'abc', it prints 'a'. 

if (buffer == 'a'){
    char copy = '?';
    write(file,&copy,1); //text file containing 'abc' puts '?' were 'b' is.
    }

close(file);
}

The file “data” contains abc, i want to replace a with ? and make it ?bc but i’m getting a?c

read() is reading the right char, but write() is writing to the next char.
Why is this?

Been searching google for hours.

Thanks

  • 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-01T20:31:18+00:00Added an answer on June 1, 2026 at 8:31 pm

    The answer is actually embedded in your own code, in a way.

    The lseek call you do right after open is not required because when you first open a file the current seek offset is zero.

    After each successful read or write operation, the seek offset moves forward by the number of bytes read/written. (If you add O_APPEND to your open the seek offset also moves just before each write, to the current-end-of-file, but that’s not relevant at this point.)

    Since you successfully read one byte, your seek offset moves from 0 to 1. If you want to put it back to 0, you must do that manually.

    (You should also check that each operation actually succeeds, of course, but I assume you left that out for brevity here.)

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

Sidebar

Related Questions

I need to replace a particular character in a text file with another character.
I need to replace some text in a JNLP file using a DOS batch
I have text like this template template template_results template And I need to replace
I have a text file which contains data I need to preload into a
I need to delete any character that's not 0 through 9, a through z,
I need to replace a simple string in a minified .js file after a
I'm using Powershell for replace a string in a text file. For replace String
I have a relatively large csv/text data file (33mb) that I need to do
In a pipe separated text file I need to trim the first column to
I need to replace accented characters in a string with their unaccented counterparts and

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.