
Finding the First Line of Data in a Plain Text Data File


hrodstein
// This snippet illustrates how to load data from a text file when there is a variable // length header before the data. The technique is to read the file line-by-line // using FReadLine, looking for the first data line. In this case the header ends // with a line that starts like this: // "[DATA...] // We look for that pattern to identify where the data starts. Function FindFirstDataLine(pathName, filePath) String pathName // Name of symbolic path or "" String filePath // Name of file or partial path relative to symbolic path. Variable refNum Open/R/P=$pathName refNum as filePath String buffer, text Variable line = 0 do FReadLine refNum, buffer if (strlen(buffer) == 0) Close refNum return -1 // The expected keyword was not found in the file endif text = buffer[0,4] if (CmpStr(text, "[DATA") == 0) // Line does start with "[DATA" ? Close refNum return line + 1 // Success: The next line is the first data line. endif line += 1 while(1) return -1 // We will never get here End Function LoadDataFile(pathName, filePath, extension) String pathName // Name of symbolic path or "" to display dialog. String filePath // Name of file or "" to display dialog. Can also be full or partial path relative to symbolic path. String extension // e.g., ".dat" for .dat files. "????" for all files. Variable refNum // Possibly display Open File dialog. if ((strlen(pathName)==0) || (strlen(filePath)==0)) Open /D /R /P=$pathName /T=(extension) refNum as filePath filePath = S_fileName // S_fileName is set by Open/D if (strlen(filePath) == 0) // User cancelled? return -1 endif // filePath is now a full path to the file. endif Variable firstDataLine = FindFirstDataLine(pathName, filePath) if (firstDataLine < 0) Printf "No data found in file %s\r", filePath return -1 endif LoadWave /J /D /O /E=1 /K=0 /L={0,firstDataLine,0,0,0} /P=$pathName filePath return 0 End

Forum

Support

Gallery
Igor Pro 9
Learn More
Igor XOP Toolkit
Learn More
Igor NIDAQ Tools MX
Learn More