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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:29:41+00:00 2026-05-25T23:29:41+00:00

I stumbled on a strange problem when trying to port a piece of C++

  • 0

I stumbled on a strange problem when trying to port a piece of C++ code from MSWindows to linux (Mint10). I discovered that there was a little typo in a hard-coded filename : a trailing space char (\x20).

The code worked like a charm under Windows (Windows Vista/7), but it had trouble to deal with the erroneous filename under linux. I discovered that both std::filebuf::open and std::fopen (and also boost::filesystem::filebuf) allow trailing space chars in filename under Windows (which I think is an incorrect behavior).

I tested the following code using MSVC2010 and MinGW under Win7 :

# include <iostream>
# include <fstream>
# include <string>
# include <cstdlib>

const std::string filename = "Z:\\Path\\To\\A\\File " ;  // one trailing space

int main(int, char * [])
{
    std::cout << "Testing with file : \"" << filename << "\"" << std::endl ;

    // Testing with std::filebuf::open
    std::filebuf fb ;
    fb.open(filename.c_str(), std::ios::in | std::ios::binary) ;
    if( fb.is_open() )
    {
        std::cout << "Using std::filebuf : Opened" << std::endl ;
        fb.close() ;
    }
    else
    {
        std::cout << "Using std::filebuf : Not opened" << std::endl ;
    }

    // Testing with std::fopen
    std::FILE * fp = std::fopen(filename.c_str(), "rb") ;
    if( fp )
    {
        std::cout << "Using std::fopen : Opened" << std::endl ;
        std::fclose(fp) ;
    }
    else
    {
        std::cout << "Using std::fopen : Not opened" << std::endl ;
    }
    return 0 ;
}

I verified that the file on the harddrive has no trailing space characters in the name. The file was successfully opened on Windows, but no luck to access it under linux if the filename isn’t an exact match.

So I have a couple of questions :

  • Is this a normal behavior of the file API under Windows ? And is there some other gotchas ? I googled but found no reference on this case. (Maybe I’m a bad googler :))
  • How to write a code that works in a strict fashion with filenames ? Always trimming filenames before passing them to some file function isn’t the solution for my case ; my current app have to list directories (using boost::filesystem::directory_iterator) and trimming filenames will break the app if the user provides filenames with trailing space chars (which is rare but allowed on many file systems).

Thanks for any advice !

  • 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-25T23:29:42+00:00Added an answer on May 25, 2026 at 11:29 pm

    Windows itself will strip the trailing blanks from a filename, it has nothing to do with the C++ function you use to open the file.

    Here’s one reference to this behavior in the Windows API function CreateFile: INFO: Filenames Ending with Space or Period Not Supported

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

Sidebar

Related Questions

I've stumbled onto a very strange bug. Read the comments in the code to
I stumbled across a little problem with an ASP.NET Web Application. I got a
I stumbled over some code that appears to be correct. It is supposed to
I've stumbled on this strange issue that I never had before; On my website
I stumbled upon a very strange bit of PHP code. Could someone explain why
I've stumbled upon a rather strange issue today with Spring 3.0: There's an abstract
I stumbled upon this question from two years ago. Is there a way to
I stumbled about a strange problem with ListViews. In the emulator the ListView shows
I've stumbled upon a strange problem. I made a regular os x system update
I stumbled upon a strange error C2440: '' : cannot convert from '_CR' to

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.