Author Topic: [PCL - C++] Write plain text, log file.  (Read 6359 times)

Offline Carlos Milovic

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2172
  • Join the dark side... we have cookies
    • http://www.astrophoto.cl
[PCL - C++] Write plain text, log file.
« on: 2010 December 14 10:03:33 »
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: [Select]

  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.

Regards,

Carlos Milovic F.
--------------------------------
PixInsight Project Developer
http://www.pixinsight.com

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: [PCL - C++] Write plain text, log file.
« Reply #1 on: 2010 December 14 12:05:14 »
Hola Carlos

Try this:

Code: [Select]
 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!
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Carlos Milovic

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2172
  • Join the dark side... we have cookies
    • http://www.astrophoto.cl
Re: [PCL - C++] Write plain text, log file.
« Reply #2 on: 2010 December 14 12:27:15 »
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... ;)


Regards,

Carlos Milovic F.
--------------------------------
PixInsight Project Developer
http://www.pixinsight.com