在Unix下创建的文件上使用windows下的fstream::seekg 我在Linux下用C++编写了一个跨平台的程序,在PC机下用Visual Studio编写。这个程序使用< P>将文本行写到文本文件中。我不能轻易地升级我的项目的运行库,显然,没有其他的解决方案。
我试图在文件打开时设置std::ios\u base::binary属性。它修复了报告的问题,但引入了一个新问题:当使用getline读取文件时,我们会得到额外的\r字符 因此,如果有人有相同的问题并需要修复,这里有一个解决方法:只需关闭文件,重新打开它,然后吃掉前n个字符,将读取指针移动到正确的位置:在Unix下创建的文件上使用windows下的fstream::seekg 我在Linux下用C++编写了一个跨平台的程序,在PC机下用Visual Studio编写。这个程序使用< P>将文本行写到文本文件中。我不能轻易地升级我的项目的运行库,显然,没有其他的解决方案。,c++,linux,windows,fstream,eol,C++,Linux,Windows,Fstream,Eol,我试图在文件打开时设置std::ios\u base::binary属性。它修复了报告的问题,但引入了一个新问题:当使用getline读取文件时,我们会得到额外的\r字符 因此,如果有人有相同的问题并需要修复,这里有一个解决方法:只需关闭文件,重新打开它,然后吃掉前n个字符,将读取指针移动到正确的位置: #include <fstream> #include <iostream> #include <string> #include <assert.h&
#include <fstream>
#include <iostream>
#include <string>
#include <assert.h>
int main()
{
std::fstream file;
const std::string fileName = "buglines.txt";
file.open( fileName.c_str(), std::ios_base::in );
if ( file.is_open() )
{
std::streampos posLine2;
std::string lineStr;
std::string line2Str;
int line = 1;
while ( std::getline( file, lineStr ) )
{
if ( line == 1 )
posLine2 = file.tellg(); // save line 2 position
if ( line == 2 )
line2Str = lineStr; // save line 2 content
++line;
std::cout << lineStr << std::endl;
}
std::cout << "Reached EOF, trying to read line 2 a second time" << std::endl;
//file.clear(); // clear EOF flag
//file.seekg(posLine2); // move to line 2
file.close();
file.open( fileName.c_str(), std::ios_base::in );
assert( file.is_open() );
char* temp = new char[static_cast<int>(posLine2)+1];
file.read( temp, static_cast<int>(posLine2)+1 ); // if posLine2 is too big, consider splitting with in a loop
delete [] temp;
assert( file.tellg() == posLine2 );
std::getline( file, lineStr ); // read the line
assert( lineStr == line2Str ); // compare
}
return 0;
}
我不能为我的项目轻松升级运行库,显然,没有其他解决方案 我试图在文件打开时设置std::ios\u base::binary属性。它修复了报告的问题,但引入了一个新问题:当使用getline读取文件时,我们会得到额外的\r字符 因此,如果有人有相同的问题并需要修复,这里有一个解决方法:只需关闭文件,重新打开它,然后吃掉前n个字符,将读取指针移动到正确的位置:
#include <fstream>
#include <iostream>
#include <string>
#include <assert.h>
int main()
{
std::fstream file;
const std::string fileName = "buglines.txt";
file.open( fileName.c_str(), std::ios_base::in );
if ( file.is_open() )
{
std::streampos posLine2;
std::string lineStr;
std::string line2Str;
int line = 1;
while ( std::getline( file, lineStr ) )
{
if ( line == 1 )
posLine2 = file.tellg(); // save line 2 position
if ( line == 2 )
line2Str = lineStr; // save line 2 content
++line;
std::cout << lineStr << std::endl;
}
std::cout << "Reached EOF, trying to read line 2 a second time" << std::endl;
//file.clear(); // clear EOF flag
//file.seekg(posLine2); // move to line 2
file.close();
file.open( fileName.c_str(), std::ios_base::in );
assert( file.is_open() );
char* temp = new char[static_cast<int>(posLine2)+1];
file.read( temp, static_cast<int>(posLine2)+1 ); // if posLine2 is too big, consider splitting with in a loop
delete [] temp;
assert( file.tellg() == posLine2 );
std::getline( file, lineStr ); // read the line
assert( lineStr == line2Str ); // compare
}
return 0;
}
您是否正在使用mingw将windows二进制化?否,Visual Studio 2010 SP1它也链接了哪个版本的msvcrt?msvcrt.dll:7.0.7601.17744该版本可能存在错误,至少根据旧版本,请尝试链接到更新的版本,VS 2010应该与10.0.something捆绑在一起。您是否使用mingw将windows二进制化?不,Visual Studio 2010 SP1它也链接了哪个版本的msvcrt?msvcrt.dll:7.0.7601.17744该版本可能存在错误,至少根据旧版本,请尝试链接到更新的版本,VS2010应该与10.0.0版本捆绑在一起。