C++ Windows上的字符串大小与Linux上的不同

C++ Windows上的字符串大小与Linux上的不同,c++,windows,linux,string,size,C++,Windows,Linux,String,Size,我偶然发现了字符串::substr的奇怪行为。通常我在Eclipse+MinGW中使用windows7编写代码,但当我在笔记本电脑上使用Linux(ubuntu12.04)中的Eclipse时,我注意到结果的不同 我正在使用填充了一行行文本的向量。其中一个步骤是从行中删除最后一个字符 在win7 Eclipse中,我做到了: for( int i = 0; i < (int)vectorOfLines.size(); i++ ) { vectorOfTrimmedLines.pus

我偶然发现了字符串::substr的奇怪行为。通常我在Eclipse+MinGW中使用windows7编写代码,但当我在笔记本电脑上使用Linux(ubuntu12.04)中的Eclipse时,我注意到结果的不同

我正在使用填充了一行行文本的向量。其中一个步骤是从行中删除最后一个字符

在win7 Eclipse中,我做到了:

for( int i = 0; i < (int)vectorOfLines.size(); i++ )
{
    vectorOfTrimmedLines.push_back( ((string)vectorOfLines.at(i)).substr(0, ((string)vectorOfLines.at(i)).size()-1) );
}
或使用另一种方法:

vectorOfTrimmedLines.push_back( ((string)vectorOfLines.at(i)).replace( (((string)vectorOfLines.at(i)).size()-2),1,"",0 ));
当然,Linux方法在windows上的工作方式是错误的(修剪最后两个字符,或者替换最后一个字符之前的一个)

问题似乎是myString.size()在Windows中返回字符数,但在Linux中返回字符数+1。 在Linux上是否会计算新行字符

作为C++中的新手和编程一般,我想知道为什么会这样,如何做到独立于平台。 我想知道的另一件事是:哪种方法更可取(更快)substr还是replace

编辑: 用于填充字符串的方法。我编写了此函数:

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
}
vectorReadFile(字符串路径文件)
{
//打开文件
ifstreammyfile;
myFile.open(pathToFile.c_str());
//此函数返回的字符串向量逐行包含文件
向量<字符串>向量线;
//检查文件是否打开,然后逐行读取矢量的字符串元素
如果(myFile.is_open())
{
字符串行;//这将包含从当前文件读取的数据
while(getline(myFile,line))//直到文件中的最后一行
{
vectorOfLines.push_back(line);//将当前行添加到向量中的新字符串元素
}
myFile.close();//关闭文件
}
//如果文件不存在
其他的
{

cerr文本文件在不同的操作系统上不相同。Windows使用两个字节的代码来标记行的结尾:0x0D,0x0A。Linux使用一个字节,0x0A。
getline
(以及大多数其他输入函数)在读取字符时知道编译它的操作系统的约定OS用来表示行尾的字符,它将字符替换为“\n”。因此,如果在Windows下编写文本文件,行尾为0x0D,0x0A;如果在Linux下读取该文本文件,
getline
将0x0D视为普通字符,然后将0x0A视为行尾


因此,寓意是当您将文本文件从一个系统移动到另一个系统时,必须将其转换为本机表示。
ftp
知道如何做到这一点。如果您在虚拟机中运行,则必须在切换系统时手动进行转换。从Unix命令行使用
tr
非常简单。

这是因为在Windows中,换行符由两个字符CR+LF表示,而在Linux上,换行符仅为LF,在Mac(OSX之前)上,换行符仅为CR

只要您只使用Linux系统上Linux生成的文件或Windows系统上Windows生成的文件,您就不必担心。但只要您需要使用Windows系统上Linux生成的文件或Windows系统上Linux生成的文件,您就需要正确处理换行符

第一步,您需要以二进制模式打开文件
std::ofstream infle(“filename”,std::ios_base::binary);
,然后有三个选项:

  • 您需要为所有平台确定一个单一的换行规则,并始终如一地使用它
  • 您需要能够检测当前文件中使用的换行符约定(通常通过检查第一行中使用的换行符来实现),将其保存在变量中,并将其传递给需要处理换行符的字符串函数
  • 告诉用户将文件转换为正确的换行符,例如使用dos2unix和unix2dos,或者如果文件传输涉及FTP,则使用ASCII模式

  • 或者,如前所述,使用Boost。

    在Windows和Linux/Unix中,行尾不一样-Windows使用两个字节,Linux使用一个。Google如何在.nix命令行上使用tr,您将看到如何转换它们


    祝你好运!

    省去一些压力,首先使用Show填充字符串的方法。为什么使用打字?为什么使用
    at
    而不是
    []
    operator?为什么不在循环中使用迭代器?最后,你确定Linux中的字符串与Windows中的字符串是一样的吗?@JoachimPileborg-我猜Linux中的字符串与Windows中的字符串完全相同,但它们来自Windows下编写的文本文件,因此有两个字符表示newline、 “Linux上是否会计算新行字符?”-如果它在字符串中,它总是会被计算在内,因为它是一个普通字符。我想问题在于,您的字符串在一开始就有不同的值。请注意!这一定是原因。感谢您的教训!在我接受其他响应后只落后几秒钟,但感谢您的正确输入!
    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
    }