Hi Nalaka,
In the topic I cited earlier the header length was determined by locating the string "[Data]". You need to find the first line that does not begin with the character '#'. It took a few simple modifications to the earlier code to make it work for you. Refer to the Programming Guide for instructions on how to use the code below.
Help > Programming > Code Builder User's Guide > Working with Files and Origin C Compiler
// code starts herevoid test_ascimp()
{
int iNumSelFiles;
string strPath,strHeader;
StringArray saFiletypes,saFilePaths;
saFiletypes.SetSize( 2 );
saFiletypes[0]="[data (*.dat)] *.dat";
saFiletypes[1]="[all (*.*)] *.*";
iNumSelFiles = GetMultiOpenBox( saFilePaths, saFiletypes );
for (int i = 0; i < iNumSelFiles; i++)
{
// Find where header stops and retrieve its text
int nHdrLines = get_header(saFilePaths[i], '#', strHeader);
if( nHdrLines < 1)
{
printf("%d\m", nHdrLines);
out_str("Failed to find marker in file");
continue;
}
// Create new worksheet
WorksheetPage wpg;
wpg.Create("Origin");
Worksheet wks = wpg.Layers(0);
// Get current ascimp settings from worksheet
ASCIMP ascimp;
wks.GetASCIMP(ascimp);
// Set header lines
ascimp.iHeaderLines = nHdrLines;
// Import file
int iRet = wks.ImportASCII(saFilePaths[i], ascimp);
if( iRet != 0 )
{
printf("Import failed with error: %d\n", iRet);
wpg.Destroy();
}
else
{
// create text label from header string
LT_execute("label -s -sa " + strHeader);
}
}
}
//--------------------------------------------------------------------------------------
// Function to read file and look for a line that does not begin with the marker
// Note that the search for marker is case sensitive
static int get_header(string strFile, char strMarker, string& strHeader)
{
stdioFile ff;
bool bRet = ff.Open(strFile, file::modeRead);
if( !bRet ) return -1;
int nMarkerLine = -1;
string strTemp;
int iLine;
while( ff.ReadString(strTemp) )
{
if( strTemp.Find(strMarker) != 0 )
{
nMarkerLine = iLine;
break;
}
iLine++;
// add line to header string
strHeader.Format("%s\n%s", strHeader, strTemp);
}
ff.Close();
return nMarkerLine;
}
Mike Buess
Origin WebRing Member