Plain Text Files
To read and write from plain text files involves reading and writing strings of data. These text strings can be manipulated with built-in string functions, like RTrim, Find, Replace, and so on.
Note: If your data is hierarchical in nature, or based on existing PeopleSoft records or pages, use a File Layout definition for reading and writing your data, rather than doing it line by line (or field by field.)
The following example creates an array of array of string, then reads in two files, one into each "column" of the array. The Names file contains names; the Numbers file contains employee numbers. The ReadLine method reads each successive line in a file, until it reaches the end of the file.
Notice that the first file is opened using the GetFile function. The second file is not opened using GetFile, but rather with the Open method. After the data is read into the array, you can do processing on the data. The end of the program writes the changes back to the files, using the WriteLine method, which includes a system end of line character at the end of every line.
Local array of array of string &BOTH; Local File &MYFILE; Local string &HOLDER; /* Create empty &BOTH array */ &BOTH = CreateArrayRept(CreateArrayRept("", 0), 0); /* Read first file into first column */ &MYFILE = GetFile("names.txt", "R"); While &MYFILE.ReadLine(&HOLDER); &BOTH.Push(&HOLDER); End-While; /* read second file into second column */ &MYFILE.Open("numbers.txt", "R"); &LINENO = 1; While &MYFILE.ReadLine(&HOLDER); If &LINENO > &BOTH.Len Then /* more number lines than names, use a null name */ &BOTH.Push(CreateArray("", &HOLDER)); Else &BOTH[&LINENO].Push(&HOLDER); End-If; &LINENO = &LINENO + 1; End-While; /* if more names than numbers, add null numbers */ For &LINENO = &LINENO to &BOTH.Len &BOTH[&LINENO].Push(""); End-For; &MYFILE.Close(); /* do processing with array */ /* write data back to files */ &MYFILE1 = GetFile("names.txt", "A"); &MYFILE2 = GetFile("numbers.txt", "A"); /* loop through array and write to files */ For &I = 1 To &BOTH.Len &STRING1 = &BOTH[&I]; &MYFILE1.writeline(&STRING1); &STRING2 = &BOTH[&I]; &MYFILE2.writeline(&STRING2); End-For; &MYFILE1.Close(); &MYFILE2.Close();