Source file: /~heha/mb-iwp/NA/MSVC.zip/src/SaveTsv.cpp

#include "PiezoMess.h"
#include <commdlg.h>

// Paar aus Amplitude und Phase tab-separiert ausschreiben
static void WritePair(FILE*f, SDATA*data, int i) {
 char s[16];
 float scale=16.0F/adcmean.ref;
 FloatToStringA(sweep.amp.FromNative(1.0F-data[i].amp()*scale),2,s,elemof(s));
 fprintf(f,"\t%s",s);
 FloatToStringA(sweep.pha.FromNative(1.0F+data[i].pha()*scale),1,s,elemof(s));
 fprintf(f,"\t%s",s);
}

// CSV-Datei (hier: tab-separiert) speichern, kompatibel zu LabVIEW-Programmen und DMM.exe
void SaveCSV() {
 OPENFILENAME ofn;
 InitStruct(&ofn,sizeof(ofn));
 ofn.hwndOwner=ghMainWnd;
 ofn.lpstrFile=gCsvName;
 ofn.nMaxFile=elemof(gCsvName);
 ofn.lpstrFilter=T("CSV-Dateien\0*.tsv;*.csv\0");
 ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT;
 ofn.lpstrDefExt=T("tsv");
 if (GetSaveFileName(&ofn)) for(;;){
  bool ok=false;
  FILE *f = _tfopen(gCsvName,T("w"));
  if (f) {
   SYSTEMTIME st;
   TIME_ZONE_INFORMATION tz;
   char s[16];
   int i,j=0;
   GetLocalTime(&st);
   switch (GetTimeZoneInformation(&tz)) {
    case TIME_ZONE_ID_STANDARD: j=tz.StandardBias; break;
    case TIME_ZONE_ID_DAYLIGHT: j=tz.DaylightBias; break;
   }
   j+=tz.Bias;
   FloatToStringA((float)j/60,2,s,elemof(s));
// Tabellenzeile 1: Uhrzeit der Messung	2012/07/25 14:46:22,234 -1,00
   fprintf(f,"%d/%02d/%02d %02d:%02d:%02d%c%03d %s\n",
     st.wYear,st.wMonth,st.wDay,
     st.wHour,st.wMinute,st.wSecond,
     sDecimal[0],st.wMilliseconds,s);
// Tabellenzeile 2: Variablennamen	freq	amp	pha	amp0	pha0	amp1	pha1	à
   fprintf(f,"freq\tamp\tpha");
   for (i=0; i<sweep.nbackup; i++) {
    fprintf(f,"\tamp%d\tpha%d",i,i);
   }
   fprintf(f,"\n");
// Tabellenzeile 3: Einheiten		MHz	dB	deg	dB	deg	dB	deg	à
   fprintf(f,"MHz");
   for (i=0; i<sweep.nbackup+1; i++) {
    fprintf(f,"\tdB\tdeg");
   }
   fprintf(f,"\n");
// weitere Zeilen: Messwerte		200,23	20,2	160	10,2	170	-20,4	140	à
   for (i=0; i<sweep.width; i++) {
    FloatToStringA(sweep.x.FromNative((float)i/(sweep.width-1)),2,s,elemof(s));
    fprintf(f,"%s",s);
    if (i<sweep.nsamp) WritePair(f,sweep.data,i); else fprintf(f,"\t\t");
    for (j=0; j<sweep.nbackup; j++) WritePair(f,sweep.backup[j],i);
    fprintf(f,"\n");
   }
   if (!fflush(f)) ok=true;	// fflush() sollte hier -1 liefern wenn Festplatte voll
   fclose(f);
   if (!ok) _tunlink(gCsvName);	// Leichenbeseitigung
  }
  if (ok || MBox(ghMainWnd,(PTSTR)9,
	MB_ICONERROR|MB_RETRYCANCEL|MB_Sound|MB_ErrorText,
	GetLastError(),gCsvName)!=IDRETRY) break;
 }
}
Detected encoding: OEM (CP437)1
Wrong umlauts? - Assume file is ANSI (CP1252) encoded