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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:43:42+00:00 2026-06-12T16:43:42+00:00

I stumbled upon strange behavior of string::substr . Normally I code on Windows 7

  • 0

I stumbled upon strange behavior of string::substr. Normally I code on Windows 7 in Eclipse+MinGW, but when I was working on my laptop, using Eclipse in Linux (Ubuntu 12.04) I noticed difference in result.

I was working with vector< string > filled with lines of text. One of steps was to remove last character from line.

In win7 Eclipse I did:

for( int i = 0; i < (int)vectorOfLines.size(); i++ )
{
    vectorOfTrimmedLines.push_back( ((string)vectorOfLines.at(i)).substr(0, ((string)vectorOfLines.at(i)).size()-1) );
}

and it works like intended (removing last character from each line)

But in Linux this code do not trim. Instead I needed to do it like this:

//  -2 instead -1 character
vectorOfTrimmedLines.push_back( ((string)vectorOfLines.at(i)).substr(0, ((string)vectorOfLines.at(i)).size()-2) );

or using another method:

vectorOfTrimmedLines.push_back( ((string)vectorOfLines.at(i)).replace( (((string)vectorOfLines.at(i)).size()-2),1,"",0 ));

Ofcourse Linux methods work wrong way on windows (trimming 2 last characters, or replacing one before last).

The problem seems to be that myString.size() return number of characters in Windows, but in Linux it returns number of characters + 1.
Could it be that new line character is counted on Linux?

As a newbie in C++ and programming general, I wonder why it is like that, and how can this be done to be platform independent.

Another thing that I wonder is : which method is preferable (faster) substr or replace?

Edit:
Method used to fill string s this function i wrote:

vector< string > ReadFile( string pathToFile )
{
    //  opening file
    ifstream myFile;
    myFile.open( pathToFile.c_str() );

    //  vector of strings that is returned by this function, contains file line by line
    vector< string > vectorOfLines;

    //  check if the file is open and then read file line by line to string element of vector
    if( myFile.is_open() )
    {
        string line;    //  this will contain the data read from current the file

        while( getline( myFile, line ) )    //  until last line in file
        {
            vectorOfLines.push_back( line );    //  add current line to new string element in vector
        }

        myFile.close(); //  close the file
    }

    //  if file does not exist
    else
    {
        cerr << "Unable to open file." << endl; //  if the file is not open output
        //throw;
    }

    return vectorOfLines;   //  return vector of lines from file
}
  • 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-06-12T16:43:43+00:00Added an answer on June 12, 2026 at 4:43 pm

    Text files are not identical on different operating systems. Windows uses a two-byte code to mark the end of a line: 0x0D, 0x0A. Linux uses one byte, 0x0A. getline (and most other input functions) knows the convention for the OS that it was compiled for; when it reads the character(s) that the OS uses to represent the end of a line, it replaces the character(s) with ‘\n’. So if you write a text file under Windows, lines end with 0x0D, 0x0A; if you read that text file under Linux, getline sees 0x0D and treats it as a normal character, then it sees 0x0A, and treats it as the end of the line.

    So the moral is that you must convert text files to the native representation when you move them from one system to another. ftp knows how to do this. If you’re running in a virtual box, you have to do the conversion manually when you switch systems. It’s simple enough with tr from a Unix command line.

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

Sidebar

Related Questions

I stumbled upon a very strange bit of PHP code. Could someone explain why
Our project does some Java bytecode instrumentation. And we stumbled upon some strange behavior.
Making a footer for a web site I stumbled upon some strange behavior of
I stumbled upon this code: static void Main() { typeof(string).GetField(Empty).SetValue(null, evil);//from DailyWTF Console.WriteLine(String.Empty);//check //how
I'm new to WPF and its Databinding, but I stumbled upon a strange behaviour
I stumbled upon this piece of code which seems totaly broken to me, but
I have stumbled upon a strange behavior that I don't understand. I have to
This is a bit a silly question, but I stumbled upon this strange character
I've just stumbled upon what I consider strange behavior which might well just be
I stumbled upon https://codereview.stackexchange.com/questions/10610/refactoring-javascript-into-pure-functions-to-make-code-more-readable-and-mainta and I don't understand the answer since the user uses

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.