0

I am reading filenames written in a text file into a string vector. For each file name I want to create its complete path so that I can read it. For final path I merge 'Map_PATH' and 'filenames' variables together into FileName (char array) using sprintf function.

Now on windows its shows FileName contains "/data2/worcester_000187.txt" which is what I needed. But running it on Ubuntu server gives a result ".txta2/worcester_000187" instead, it overwritten data2 partially. Text file contains file names like this i.e. Single name on each line:

worcester_000187
worcester_000192
worcester_000193
worcester_000194
worcester_000196
worcester_000197
worcester_000198

What I found that if I assign 'filename' string variable with a hard coded values say:

filename="worcester_000187"; 

I get the right results. But it creates problem when same filename variable gets a string values read from text file. Either there is some problem with reading with file.

Below a highlight of code is given:

char FileName[500];
char Map_PATH[]="/data2/";
vector < string > fileList;
string filename;
fstream fp;

fp.open ("ImagesListTemp.txt", ios::in);
if(!fp.is_open())
{
cerr <<"Unable to open Image Names List: "<<endl;
exit(1);
}


while (getline(fp, filename)) 
{
fileList.push_back(filename);
}
fp.close();



for(int i=0;i<fileList.size();i++)
{
 filename=fileList[i];
 //    filename="worcester_000187"; 
 sprintf(FileName,"%s%s.txt",Map_PATH,filename.c_str()); 
 // Open File and do some operations

}
roalz
  • 2,699
  • 3
  • 25
  • 42
NightFox
  • 125
  • 1
  • 10

1 Answers1

1

As anticipated the problem was with the text file. Here is what was happening.

  1. A text file written in windows (I wrote text file in windows) takes CR and LF (\r\n) as line ending.
  2. Linux and Unix considers just LF (\n) as line ending.
  3. When running on Linux getline() detects \n to detect end of line and filename gets "worcester_000187\r" as value. Notice extra carriage return added.
  4. Following statement sprintf(FileName,"%s%s.txt",Map_PATH,filename.c_str()) can be assessed in two steps i.e. before and after we reach to '.txt'

  5. Before '.txt' FileName contains '/data2/worcester_000187\r' . Now '\r' brings us back to start of this line again.

  6. Now we add .txt and cursor is in the start and hence it overwrites first four characters in 'FileName' array. I hope it makes sense.

Using dos2unix command converts text file to the desired format.

NightFox
  • 125
  • 1
  • 10
  • 1
    Hmm, nasty. Solved, however, I would make the file reading platform independent by *explicitly* testing for n `\r`, and removing if found. – Jongware Jan 20 '14 at 23:07
  • Following post suggests a very nice solution for that. http://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf/6089413#6089413 – NightFox Jan 21 '14 at 01:31