Join free weekly webinars
Find us on

How to read a text file in snippets

Tags: snippets, C#, file

In some experiment scenarios you may need to open a text file containing, for example, randomized number sequences. You may choose not to load a permanent copy of such file into experiment’s Library, for instance, in cases when you regenerate the file content before each experiment run. Thus, the file is stored externally to your experiment. Then, the easiest way to read the file content is to use standard C# file functions directly in code snippets. If you need to read a file only once, at the start of experiment, then using the EXPERIMENT.OnInitializing snippet might be a good idea. The example below demonstrates this process in details.

Suppose, we have a text or csv file, with the following data in three lines:

1,2.4,condition 1
3,5.6,condition 2
2,4.2, condition 4

The row values are arranged in the comma-separated columns, 1 to 3, which contain integer, double and text values, respectively.
// We start with reading text lines in the file   
// Note that the double slash is used for folder separation in C#  
string[] Lines=File.ReadAllLines("D:\\temp\\data.txt"); 
// At this point the string array, Lines, is assigned with 3 members containing 3 text lines 
// from the original file.
// However, the above code will be valid for text file with any number of lines */
// Now we need to parse each line into columns data values
// At this point we need to be aware about a structure of the file data
// We will create three arrays of different data type for values from each column in the original file
int[] FirstColumn=new int[Lines.Length];
double[] SecondColumn=new double[Lines.Length];
string[] ThirdColumn=new string[Lines.Length];
// Lets start line parsing
for (int i=0;i<Lines.Length;i++)
    // First, split the particular line into text blocks, using ',' as separator
    // and assign a temporal string array, Columns, with splitted text blocks
    // Notice that you can provide any separator, specific for your data files 
    string[] Columns=Lines[i].Split(','); 
    // Next, we need to convert textblocks into appropriate data types
    FirstColumn[i]=Convert.ToInt32(Columns[0]); // convert the first text block into the integer number
    SecondColumn[i]=Convert.ToDouble(Columns[1]); // convert the second text block into the double number
    ThirdColumn[i]=Columns[2]; // the third text block is a string variable already 
/// At this point all file data is read and converted into three arrays: 
/// FirstColumn, SecondColumn, ThirdColumn.
/// Each array contans three members, or data values, such that a member index 
/// corresponds to line numbers in the original file. 
/// The file data is available for using in the experiment!


Notice that resulting arrays, FirstColumn, SecondColumn and ThirdColumn are declared as local variables within this code snippet. If you want to access the array data globally, across all snippets in an experiment, you need to declare the arrays in Header. The change is easy. Add to the Header snippet the following code:

// Global array declaration
int[] FirstColumn;
double[] SecondColumn;
string[] ThirdColumn;


Then, change array initialization in the EXPERIMENT.OnInitializing snippet.

// Local arrays
//int[] FirstColumn=new int[Lines.Length];
//double[] SecondColumn=new double[Lines.Length];
//string[] ThirdColumn=new string[Lines.Length];
// Global arrays, as defined in the Header
FirstColumn=new int[Lines.Length];
SecondColumn=new double[Lines.Length];
ThirdColumn=new string[Lines.Length];


As a final remark, always make sure that the file exists in the its location on a disk. If you try to read non-existing file, or a file with wrong content, the experiment run will be aborted on execution of the snippet.


Best wishes, OkazoLab team.