I have C++ a cross-platform program (compiled with g++ under Linux and with Visual Studio under PC). This program writes lines to a text file (using <<
operator and std::endl
) but can also read data back from the generated text file (using std::getline
).
To optimize data access and save memory, when reading the data file, I read it a first time and save data position in my program. When data is needed, I later use seekg
to move to a specific position and read the data.
- Creating and reading the file on PC works fine.
- Creating and reading the file on Linux works fine.
- But creating the file on Linux and reading on PC fails.
Under PC, seekg sometime fails to move the cursor accordingly. I could isolate the problem in the example below. It reads the file once, saves second lineposition and value, then moves back to the saved position and reads the line again.
#include <fstream>
#include <iostream>
#include <string>
#include <assert.h>
int main()
{
std::fstream file;
file.open( "buglines.txt", 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
std::getline( file, lineStr ); // read the line
assert( lineStr == line2Str ); // compare
}
return 0;
}
I'm running this from Windows.
- If
buglines.txt
was created under Windows (hexadecimal editor shows line separators as 2 characters0x0D 0x0A
), it works (lineStr == line2Str
). - If
buglines.txt
was created under Linux (hexadecimal editor shows line separators as 1 character0x0A
), it does not works (lineStr is empty string). Even if the getline loop worked perfectly.
I know both system deals differently with EOL, but as I'm just using getline
function for reading, I was hoping that it would smartly work...am I missing something?
10.0.40219.1 SP1Rel
andmsvcrt.dll
is picked up fromC:\Windows\SysWOW64
. I checked my Visual installation folder + the whole Windows folder and there is no newer version anywhere. There is no Visual Studio SP2 available...do you mean I should migrate to Visual Studio 2012 for instance? I'm not very confident in downloading a msvcrt.dll file and replacing Windows's one.... – Stricklinmsvcrt100.dll
. Sorry not too familiar with VS. – Trimestermsvcr100.dll
(with no t) andmsvcp100.dll
but nomsvcrt100.dll
, this one's name is apparently not versioned, I only findmsvcrt.dll
. – Stricklinmsvcrt.dll
as a dependency, try dynamic linkage instead. – Trimester