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

  • Home
  • SEARCH
  • 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 6751745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:57:48+00:00 2026-05-26T12:57:48+00:00

I was given a little library by my school to do some projects in.

  • 0

I was given a little library by my school to do some projects in. The library was written with linux in mind, so I’m trying to change some things to work with my MinGW compiler. One particular program is for reading files given a URL. I had to change stat to _stat to make it work properly. Opening a file works fine but _stat seems to return the incorrect value. I’ll include the relevant code below:

#ifdef WIN32
    #define stat _stat
#endif

    //return true if the number of chars read is the same as the file size
bool IsDone() const
{
    cout << "checking if numRead " << numRead << " is fileSize " << fileSize << endl;
    return (numRead == fileSize);
}

char Read()
{
    if (IsDone())
        throw IllegalStateException("stream is done");
    else
    {
        char c;
        file.get(c);
        cout << "reading: " << c << endl;
        if (file.fail())
            throw FileException(std::string("error reading from file ") + fileName);

        ++numRead;
        return c;
    }
}

void OpenFile(string fileName)
{
    struct stat buf;
    #ifdef WIN32
        if (_stat(fileName.c_str(), &buf) < 0){
            switch (errno){
                case ENOENT:
                  throw FileException(std::string("Could not find file ") + name);
                case EINVAL:
                    throw FileException(std::string("Invalid parameter to _stat.\n"));
                default:
                  /* Should never be reached. */
                    throw FileException(std::string("Unexpected error in _stat.\n"));
            }
        }
    #else
        if (stat(fileName.c_str(), &buf) < 0)
            throw FileException(std::string("could not determine size of file ") + fileName);
    #endif
        fileSize = buf.st_size;
        file.open(fileName.c_str());
}

If you would like to see the entire library, you can get them from here. I understand that the code is gross looking; I’m just trying to cludge a working windows version. This thing works fine on Linux; the problem is that when I read in a file on Windows, the size is 1 short for every newline that I use in the input file, so that if I have a file that looks like this:

 text

It works fine, but with:

text\r\n
 

It breaks, and the output looks like this:

checking if numRead 0 is fileSize 6
checking if numRead 0 is fileSize 6
reading: t
checking if numRead 1 is fileSize 6
checking if numRead 1 is fileSize 6
reading: e
checking if numRead 2 is fileSize 6
checking if numRead 2 is fileSize 6
reading: x
checking if numRead 3 is fileSize 6
checking if numRead 3 is fileSize 6
reading: t
checking if numRead 4 is fileSize 6
checking if numRead 4 is fileSize 6
reading:

checking if numRead 5 is fileSize 6
checking if numRead 5 is fileSize 6
reading:
File Error: error reading from file H:/test/data/stuff.txt

It breaks because IsDone() falsely returns false (no pun intented), and the program tries to read past the end of the file. Any suggestions on why _stat is returning an incorrect number when there’s a newline?

  • 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-26T12:57:49+00:00Added an answer on May 26, 2026 at 12:57 pm

    What _stat is returning is quite correct. Windows uses “\r\n” to signal the end of a line, but when you open a file in text mode, that will be converted to a single new-line character as you read the stream.

    If you want the stream you read to match the external length, open the file in binary mode instead.

    If you’ll pardon my saying so, once you’re done with that, my advice would be to throw this code away, and change your name so if somebody sees your post, they won’t blame you for it. What you have here is a lot of code that seems, at least to me, to make a simple task considerably more complex and difficult.

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

Sidebar

Related Questions

I am trying to make the installation of boost library a little bit slower,
I'm making my own little library of handy functions and I'm trying to follow
We are developing a little application that given a directory with PDF files creates
Hi I need a little help: How to get profile URL given an UID?
A little stuck here. I have a simple question I guess. Given the following
I'm in the process of making some changes to a library that I'm using.
A little background: We're building a library/framework for working with scientific models. We have
I know very little about JavaScript but despite this I'm trying to cobble something
i have a little problem . There are some Numerical analyze program on web
I've been trying to build a little bot which will be using Reddit's API.

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.