[PCL - C++] Write plain text, log file.

Carlos Milovic

Well-known member
Hi guys

I need a hand here ;) I'm trying to create a new text file, containing the same output as I throw to the console, to work as a log file for the data extraction I'm doing with the images.
So far, I wrote that kind of works:

Code:
  File logFile;
  logFile.OpenOrCreate( outputFilePath );
  logFile.Write( "PreCalcium Log File\n\n\n" );
  logFile.Write( "_maximum Auxiliary File - Scale Factors\n");
  logFile.Write( String().Format( "[0] Maximum value            : %u \n", scale[0] ) );
  logFile.Write( String().Format( "[1] Time scale               : %u \n", scale[1] ) );
  logFile.Write( String().Format( "[2] Average value            : %u \n", scale[2] ) );
  logFile.Write( String().Format( "[3] Standard deviation value : %u \n", scale[3] ) );
  logFile.Write( "Total Calcium at each frame:\n" );
  for (int k = 0; k < mean.Length(); ++k)
    logFile.Write( String().Format( "%u - %u", k,mean[k]) );
  logFile.Close();

The file is created fine, and the output text seems to be writed fine, but there are lots of weird characters (file attached). Am I doing something wrong? I tried to use previously ofstream, but it refused to work within my code.

 

Attachments

  • precalcium.txt
    4.7 KB · Views: 54
Hola Carlos

Try this:

Code:
  File logFile;
  logFile.OpenOrCreate( outputFilePath );
  logFile.OutTextLn( "PreCalcium Log File" );
  logFile.OutTextLn( "" );
  logFile.OutTextLn( "" );
  logFile.OutTextLn( "_maximum Auxiliary File - Scale Factors" );
  logFile.OutTextLn( IsoString().Format( "[0] Maximum value            : %u", scale[0] ) );
  logFile.OutTextLn( IsoString().Format( "[1] Time scale               : %u", scale[1] ) );
  logFile.OutTextLn( IsoString().Format( "[2] Average value            : %u", scale[2] ) );
  logFile.OutTextLn( IsoString().Format( "[3] Standard deviation value : %u", scale[3] ) );
  logFile.OutTextLn( "Total Calcium at each frame:" );
  for (int k = 0; k < mean.Length(); ++k)
    logFile.OutTextLn( IsoString().Format( "%u - %u", k, mean[k] ) );
  logFile.Close();

The File::OutText() family of functions allow you to write plain text just as you want. OutTextLn() appends a newline (UNIX convention by default) to the text written, so you don't have to bother with \n escape sequences.

On the other hand, you should use IsoString instead of String. What you want to write is a text file encoded as UTF-8 or ISO-8859-1 (=IsoString), not as UTF-16 (=String), or you'll have problems. Since your text does not include non-ASCII characters, you don't have to bother with UTF-16 -> UTF-8 conversions; otherwise you should generate a String and then write it converted to UTF-8 (String::ToUTF8()).

Finally, if what you want to do is creating/overwriting a log file each time you run your code, then consider File::CreateForWriting() instead of File::OpenOrCreate(). The latter will open an existing file in case you want to append more text, but I doubt this is what you want to do.

Hope this helps!
 
Thanks for all your suggestions! :) You are right, I already am checking if the file already exist, and the user is able to overwrite it, or rename the file automatically (I use the same routine for writing images).

It is so nice having all these things inside the PCL... ;)


 
Back
Top