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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:07:52+00:00 2026-05-27T23:07:52+00:00

I wanted to read the content of a file using the read() function. I

  • 0

I wanted to read the content of a file using the read() function. I tried the following:

#define BUFFER_LENGTH (1024)

char buffer[BUFFER_LENGTH];

// The first version of the question had a typo:
// void read_file(const char filename)
// This would produce a compiler warning.
void read_file(const char *filename)
{
    ssize_t read_bytes = 0;

    // The first version had the mode in hex instead of octal.
    //
    //     int fd_in = open(filename, O_RDONLY, 0x00644);
    //
    // This does not cause problems here but it is wrong.
    // The mode is now octal (even if it is not needed).
    int fd_in = open(filename, O_RDONLY, 0644);
    if (fd_in == -1)
    {
        return;
    }

    do
    {
        read_bytes = read(fd_in, buffer, (size_t) BUFFER_LENGTH);
        printf("Read %d bytes\n", read_bytes);

        // End of file or error.
        if (read_bytes <= 0)
        {
            break;
        }
    } while (1);

    close(fd_in);
}

I am using ‘gcc (GCC) 3.4.2 (mingw-special)’ on a Windows 7 system.

The strange behaviour I get is that not all the content is read. For example, I have
a file

05.01.2012  12:28            15.838 hello.exe

and when I try to read it I get:

Read 216 bytes
Read 0 bytes

As far as I know read() should keep reading until it reaches the end of the file. While does
it report an end of file (0) the second time it is called?

Maybe I am missing something obvious but I cannot see it. I have read this document and this document over and over again and I cannot find what I am doing wrong. Does anyone have any clue?

EDIT

Thanks for the hint! It is a typo in the question (I have corrected it). It is correct in the source
code.

  • 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-27T23:07:53+00:00Added an answer on May 27, 2026 at 11:07 pm

    I suspect byte 217 to be EOF (26, 0x1A) – in Windows files can be opened in “text” or “binary” mode. In text mode, a 0x1A is interpreted as EOF.

    You would need to look at your open mode – O_BINARY. In PHP this is why you must fopen with mode “rb” (READ BINARY) and not “R” (“R” which defaults to READ TEXT).

    http://www.mingw.org/wiki/FAQ says the flag is O_BINARY (near bottom of page), so you’d need

    int fd_in = open(filename, O_RDONLY | O_BINARY, 0644);
    

    http://cygwin.com/faq.html paragraph 5.3 tells you how to handle this in cygwin

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

Sidebar

Related Questions

I wanted to read excel file so i did following steps. First creating .csv
I'm wanted to read a rss feed and store it.for this I m using:-
While trying to read an xls file using jExcelAPI, I find that the library
I have the following code to read a text file. const string FILENAME =
I am attempting to read a large XML document and I wanted to do
From what I've already read this appears to be impossible, but I wanted to
I need to un-read characters from an InputStreamReader . For that purpose I wanted
In a program that I'm writing, I wanted to make a ConfigParser that's read
The usual way to read a file in C++ is this one: std::ifstream file(file.txt,
Im still teaching myself Java so I wanted to try to read a text

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.