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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:53:16+00:00 2026-05-30T19:53:16+00:00

I was wondering.. how do you read bytes one at a time from a

  • 0

I was wondering.. how do you read bytes one at a time from a binary file? As an example, I’d like to read bytes one at a time from some binary file and then write those exact bytes to some output file (basically a very basic implementation of the cp command). My current C code looks like this:

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

int main(int argc, char**argv)
{
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
        return 1;
    }

    FILE *outfile;
    outfile = fopen(argv[1], "w");

    unsigned char c;
    char temp;
    int tracker = 0;
    // Use temp because unsigned char will never be EOF
    while ((temp = getchar()) != EOF) {
        c = (unsigned char) temp;
        fprintf(outfile, "%c", c);
    }
fclose(outfile);
return 0;
}

I run the program like this: ./main output.au < sample.au (.au is an audio file)

However, all I get in the output.au file is a bunch of “^@” repeated over and over. I can play the sample.au audio file fine, but not the output.au. I’ve also (somewhat pointlessly) done a diff on the two files, and as expected they come out differently.

Help?
Thanks!
-kstruct

UPDATE
Thanks for the reply everyone.. my code now looks like:

outfile = fopen(argv[1], "wb");

int c;
while ((c = getchar()) != EOF) {
    fprintf(outfile, "%c", c);
}
  • 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-30T19:53:18+00:00Added an answer on May 30, 2026 at 7:53 pm

    getchar() returns an int.
    You assign that int to a variable of type char thereby losing information. Don’t do that.

    Declare temp as int, and go get rid of the cast.

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

Sidebar

Related Questions

I'm just wondering how I can read a text file in php, I'd like
Im trying to get this remote binary file to read the bytes, which (of
Requirement : I must read until EOF (16 bytes a time) from a particular
I was wondering if anyone knows how to open and read from a file
I was wondering if i could safely read from an XmlDocument object using SelectNodes()
I was wondering if it was possible to read a text file that was
I just happened to read jQuery Intellisense Updates from Microsoft and was wondering if
Hi all I understand that if we read bytes from an InputStream and we
I wish to open a binary file, to read the first byte of the
I am trying to read in the pixel data from an image file as

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.