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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:49:51+00:00 2026-05-28T01:49:51+00:00

I am writing a simple program to flip all the bits in a file,

  • 0

I am writing a simple program to flip all the bits in a file, but right now it only does the first 1000 bytes until I get that much working. Why does my call to read() ignore \r characters? When I run this code on a file that only contains \r\n\r\n, the read call returns 2 and the buffer contains \n\n. The \r characters are completely ignored. I’m running this on Windows (this wouldn’t even be an issue on Linux machines)

Why does read(2) skip over the \r character when it finds it? Or is that what is happening?

EDIT: Conclusion is that windows defaults to opening files in “text” mode as opposed to “binary” mode. For this reason, when calling open, we must specify O_BINARY as the mode.

Thanks, code below.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>

void invertBytes(size_t amount, char* buffer);

int main(int argv, char** argc)
{
   int fileCount = 1;
   char* fileName;
   int fd = 0;
   size_t bufSize = 1000;
   size_t amountRead = 0;
   char* text;
   int offset = 0;

   if(argv <= 1)
   {
      printf("Usages: encode [filenames...]\n");
      return 0;
   }

   text = (char *)malloc(sizeof(char) * bufSize);

   for(fileCount = 1; fileCount < argv; fileCount++)
   {
      fileName = argc[fileCount];
      fd = open(fileName, O_RDWR);
      printf("fd: %d\n", fd);
      amountRead = read(fd, (void *)text, bufSize);
      printf("Amount read: %d\n", amountRead);
      invertBytes(amountRead, text);
      offset = (int)lseek(fd, 0, SEEK_SET);
      printf("Lseek to %d\n", offset);
      offset = write(fd, text, amountRead);
      printf("write returned %d\n", offset);
      close(fd);
   }

   return 0;
}

void invertBytes(size_t amount, char* buffer)
{
   int byteCount = 0;
   printf("amount: %d\n", amount);
   for(byteCount = 0; byteCount < amount; byteCount++)
   {
      printf("%x, ", buffer[byteCount]);
      buffer[byteCount] = ~buffer[byteCount];
      printf("%x\r\n", buffer[byteCount]);
   }
   printf("byteCount: %d\n", byteCount);
}
  • 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-28T01:49:52+00:00Added an answer on May 28, 2026 at 1:49 am
    fd = open(fileName, O_RDWR);
    

    should be

    fd = open(fileName, O_RDWR | O_BINARY);
    

    See read() only reads a few bytes from file for details.

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

Sidebar

Related Questions

I'm writing a simple program for file encryption. Mostly as an academic exercise but
I'm writing a simple program. There is only one class in it. There is
I am writing a simple program, where all the 'spaces' will be replaced by
Writing a simple program that will find exact duplicate files on my computer, but
I'm writing a simple program in Python 2.7 using pycURL library to submit file
I am writing a simple program to insert rows into a table.But when i
I'm writing a simple program using SDL, but I'm running into a strange problem.
I am writing a simple Java program to send mail, but am getting errors.
So I'm kind of a Cocoa n00b, but I'm writing this simple little program
I'm writing a simple program that reads and processes file content using a BufferedReader

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.