ASCII Read Formats

 

Description:  This document attempts to outline some examples of using the ASCII Read command.  No attempt is made here to document the syntax of the commands (that is done in the manual or on-line help), but just to show examples of how the syntax can be used.

 

In outlining these examples, we are going to look at three different scenarios:

 

ASCII input files where a SPACE is used as the delimiter.

 

Example 1

Example 2

Example 3

Example 4

 

 ASCII input files where other characters (such as ‘,’ ‘:’ etc.) are used as delimiters.

 

Example 5

Example 6

 

 Other situations.

 

Example 7

Example 8

Example 9

 

 

Example 1:  ASCII input file where the columns are separated by either 1 or multiple SPACES. In this particular example,

        the values will be read in as “numeric” variables. NOTE: Numeric values can be read into either “numeric”(%lf) or

       “string” (%s) variables.

 

            Sample Input File Format: 

 

25    30  17  45 19

 

Where we want the following values:

 

                _d_FirstValue = 25

                _d_SecondValue = 30

                _d_ThirdValue = 17

                _d_FourthValue = 45

                _d_FifthValue = 19

 

            Corresponding ASCII Read Syntax:

           

                        ASCII OPEN READONLY FILE = _s_FileName

                        _s_Format = "%lf %lf %lf %lf %lf"

                        while NOT (END OF FILE)

                         {

                              ASCII READ FILE = _s_FileName

                                 FORMAT = _s_Format

                                     VAR = _d_FirstValue, _d_SecondValue, _d_ThirdValue, _d_FourthValue, _d_FifthValue

                          }

 

            Explanation of Format:

 

                  _s_Format = "%lf %lf %lf %lf %lf"

 

                  _s_Format = “%lf = read the first number

                        _s_Format = “%lf %lf = read the next number

                        _s_Format = “%lf %lf %lf  = read the next number

                        _s_Format = “%lf %lf %lf %lf = read the next number

                        _s_Format = “%lf %lf %lf %lf %lf” = read the next number

 

 

Example 2:  ASCII input file where the columns are separated by either 1 or multiple SPACES. In this particular example,

        the values will be read into “string” variables. NOTE: Numeric values can be read into either “numeric”(%lf) or

       “string” (%s) variables.

 

            Sample Input File Format: 

 

25    30  17  45 19

 

Where we want the following values:

 

                _s_FirstValue = 25

                _s_SecondValue = 30

                _s_ThirdValue = 17

                _s_FourthValue = 45

                _s_FifthValue = 19

 

 

            Corresponding ASCII Read Syntax:

           

                        ASCII OPEN READONLY FILE = _s_FileName

                        _s_Format = "%s %s %s %s %s"

                        while NOT (END OF FILE)

                         {

                              ASCII READ FILE = _s_FileName

                                 FORMAT = _s_Format

                                     VAR = _s_FirstValue, _s_SecondValue, _s_ThirdValue, _s_FourthValue, _s_FifthValue

                          }

 

            Explanation of Format:

 

                  _s_Format = "%s %s %s %s %s"

 

                  _s_Format = “%s = read all characters up to a space

                        _s_Format = “%s %s = skip the spaces, then read all characters up to the next space

                        _s_Format = “%s %s %s  = skip the spaces, then read all characters up to the next space

                        _s_Format = “%s %s %s %s = skip the spaces, then read all characters up to the next space

                        _s_Format = “%s %s %s %s %s” = skip the spaces, then read all characters up to the next space or end of line

           

 

Example 3:  ASCII input file where the columns are separated by either 1 or multiple SPACES. In this particular example,

        the values are a combination of string and numeric but ALL will be read as “string” variables.. NOTE: Numeric values

        can be read  into either “numeric”(%lf) or  “string” (%s) variables.

 

 

            Sample Input File Format: 

 

48 Left 132 Concrete Ellipse

 

Where we want the following values:

 

                _s_Size = 48

                _s_SideOfRoad = Left

                _s_Length = 132

                _s_Material = Concrete

                _s_Shape = Ellipse

 

 

            Corresponding ASCII Read Syntax:

           

                        ASCII OPEN READONLY FILE = _s_FileName

                        _s_Format = "%s %s %s %s %s"

                        while NOT (END OF FILE)

                         {

                              ASCII READ FILE = _s_FileName

                                 FORMAT = _s_Format

                                     VAR = _s_Size, _s_SideOfRoad, _s_Length, _s_Material, _s_Shape

                          }

 

            Explanation of Format:

 

                  _s_Format = "%s %s %s %s %s"

 

                  _s_Format = “%s = read all characters up to a space

                        _s_Format = “%s %s = skip the spaces, then read all characters up to the next space

                        _s_Format = “%s %s %s  = skip the spaces, then read all characters up to the next space

                        _s_Format = “%s %s %s %s = skip the spaces, then read all characters up to the next space

                        _s_Format = “%s %s %s %s %s” = skip the spaces, then read all characters up to the next space or end of line

 

 

Example 4:  ASCII input file where the columns are separated by either 1 or multiple SPACES. In this particular example,

        the values are a combination of string and numeric and will be read in as such. NOTE: Numeric values

        can be read  into either “numeric”(%lf) or  “string” (%s) variables.

 

            Sample Input File Format: 

 

48 Left 132 Concrete Ellipse

 

Where we want the following values:

 

            _d_Size = 48

            _s_SideOfRoad = Left

            _d_Length = 132

            _s_Material = Concrete

            _s_Shape = Ellipse

 

            Corresponding ASCII Read Syntax:

           

                        ASCII OPEN READONLY FILE = _s_FileName

                        _s_Format = "%lf %s %lf %s %s"

                        while NOT (END OF FILE)

                         {

                              ASCII READ FILE = _s_FileName

                                 FORMAT = _s_Format

                                     VAR = _d_Size, _s_SideOfRoad, _d_Length, _s_Material, _s_Shape

                          }

 

            Explanation of Format:

 

                  _s_Format = "%lf %s %lf %s %s"

 

                  _s_Format = “%lf = read the first number

                        _s_Format = “%lf %s = skip the spaces, then read all characters up to the next space

                        _s_Format = “%lf %s %lf  = skip the spaces, then read the next number

                        _s_Format = “%lf %s %lf %s = skip the spaces, then read all characters up to the next space

                        _s_Format = “%lf %s %lf %s %s” = skip the spaces, then read all characters up to the next space or end of line

 

 

Example 5:  ASCII input file where the columns are separated by a character other than a space. In this particular example,

        the values are a combination of string and numeric and all values are separated by a comma (‘,’).

 

NOTE: Where the first four examples are pretty simple because the space is not seen as a character, the following

             examples are more complicated since the character  which separates the columns (the delimiter) is a valid

            character in and of itself. To take care of this, we have to replace the “%s” with the following funny-looking

            syntax:

 

           %[^,] …. Used when a ‘,’ is the delimiter

          %[^:] …. Used when a ‘:’ is the delimiter

          %[^#] … Used when the ‘#’ is the delimiter

          and so on and so forth.

                       

 

Sample Input File Format:

 

285+30,1,left,field,20,12

 

Where we want the following values:

 

            _s_CulvertStation = 285+30

            _d_Region = 1

            _s_LocationOfEntrance = left

            _s_TypeOfEntrance = field

            _d_WidthOfEntrance = 20

            _d_CulvertSize = 12

 

 

            Corresponding ASCII Read Syntax:

 

                   ASCII OPEN READONLY FILE = _s_FileName

                  _s_Format = "%[^,],%lf,%[^,],%[^,],%lf,%lf"

                  while NOT (END OF FILE)

                  {  

                      ASCII READ FILE = _s_FileName

                        FORMAT = _s_Format

                        VAR = _s_CulvertStation

                                   _d_CulvertRegion,

                                   _s_LocationOfEntrance,

                                   _s_TypeOfEntrance,

                                   _d_WidthOfEntrance,

                                   _d_CulvertSize

                   }

 

      Explanation of Format:

 

            _s_Format = "%[^,],%lf,%[^,],%[^,],%lf,%lf"

 

            _s_ Format = “%[^,] = read characters until the character ',' found

            _s_ Format = “%[^,], = skip the comma

            _s_ Format = “%[^,],%lf = read the next number

            _s_ Format = "%[^,],%lf, = skip the comma

            _s_ Format = "%[^,],%lf,%[^,] = read characters until a comma is found

            _s_ Format = "%[^,],%lf,%[^,], = skip the comma

            _s_ Format = "%[^,],%lf,%[^,],%[^,] = read characters until another comma is found

            _s_ Format = "%[^,],%lf,%[^,],%[^,], = skip the comma

            _s_ Format = "%[^,],%lf,%[^,],%[^,],%lf = read the next number

            _s_ Format = "%[^,],%lf,%[^,],%[^,],%lf, = skip the comma

            _s_ Format = "%[^,],%lf,%[^,],%[^,],%lf,%lf” = read the next number

 

 

Example 6:  ASCII input file where the columns are separated by a character other than a space. In this particular example,

        the values are a combination of string and numeric. What makes this sample different from Example 5 is that we

       use different characters as delimeters. In this case, instead of reading the station in as a single variable (285+00), we

      want to read it into two separate variables where the value to the left of the ‘+’ (285) will be read into one variable

     and the value to the right of the ‘+’ (30) will be read into another.

 

Sample Input File Format:

 

285+30,1,left,field,20,12,Circular

 

Where we want the following values:

 

            _s_CulvertStationBeforePlus = 285

            _s_CulvertStationAfterPlus = 30

            _d_CulvertRegion = 1

            _s_LocationOfEntrance = left

            _s_TypeOfEntrance = field

            _d_WidthOfEntrance = 20

            _d_CulvertSize = 12

            _s_CulvertShape = Circular

           

 

            Corresponding ASCII Read Syntax:

 

                   ASCII OPEN READONLY FILE = _s_FileName

                  _s_Format = "%[^+]+%[^,],%lf,%[^,],%[^,],%lf,%lf,%s"

                  while NOT (END OF FILE)

                  {  

                      ASCII READ FILE = _s_FileName

                        FORMAT = _s_Format

                        VAR = _s_CulvertStationBeforePlus,

                                    _s_CulvertStationAfterPlus,

                                   _d_CulvertRegion,

                                   _s_LocationOfEntrance,

                                   _s_TypeOfEntrance,

                                   _d_WidthOfEntrance,

                                   _d_CulvertSize

                                   _s_CulvertShape

                   }

 

      Explanation of Format:

 

            _s_Format = "%[^+]+%[^,],%lf,%[^,],%[^,],%lf,%lf,%s"

 

                  _s_ Format = “%[^+] = read characters until the character '+' found

            _s_ Format = “%[^+]+ = skip the ‘+’

            _s_ Format = “%[^+]+%[^,] = read characters until the character ',' found

            _s_ Format = “%[^+]+%[^,], = skip the comma

            _s_ Format = “%[^+]+%[^,],%lf = read the next number

            _s_ Format = "%[^+]+%[^,],%lf, = skip the comma

            _s_ Format = "%[^+]+%[^,],%lf,%[^,] = read characters until a comma is found

            _s_ Format = "%[^+]+%[^,],%lf,%[^,], = skip the comma

            _s_ Format = "%[^+]+%[^,],%lf,%[^,],%[^,] = read characters until another comma is found

            _s_ Format = "%[^+]+%[^,],%lf,%[^,],%[^,], = skip the comma

            _s_ Format = "%[^+]+%[^,],%lf,%[^,],%[^,],%lf = read the next number

            _s_ Format = "%[^+]+%[^,],%lf,%[^,],%[^,],%lf, = skip the comma

            _s_ Format = "%[^+]+%[^,],%lf,%[^,],%[^,],%lf,%lf = read the next number

            _s_ Format = "%[^+]+%[^,],%lf,%[^,],%[^,],%lf,%lf, = skip the comma

            _s_ Format = "%[^+]+%[^,],%lf,%[^,],%[^,],%lf,%lf,%s” = read characters until space or end of line is found

 

 

Example 7:  This is an example where the entire line is read into a single string variable.

 

Sample Input File Format:

 

VPI 1 STA 235+00 EL 175.60

 

Where we want the following values:

 

            _s_CogoCommand = VPI 1 STA 235+00 EL 175.60

 

            Corresponding ASCII Read Syntax:

 

                  _s_Format = "%[^$]"

                 while NOT (END OF FILE)

                    {

                           ASCII READ FILE = _s_FileName

                                 FORMAT = _s_Format

                                    VAR = _s_CogoCommand

                     }

 

      Explanation of Format:

 

            _s_Format = "%[^$]"

 

                  _s_ Format = “%[^$]” = read all characters up to the end of the line

 

 

      Example 8:  This is an example where you read some columns into specific variables, but then you want to read the rest

                          of the line into a single variable.

 

Sample Input File Format:

 

356+00 1  48  Ellipse 48” Culvert

 

Where we want the following values:

 

            _s_Station = 356+00

            _d_Region = 1

            _d_Size = 48

            _s_Shape = Ellipse

            _s_Description = 48” Culvert

 

            Corresponding ASCII Read Syntax:

 

                  _s_Format = "%s %lf %lf %s %[^$]"

                  while NOT (END OF FILE)

                     {

                          ASCII READ FILE = _s_FileName

                              FORMAT = _s_Format

                              VAR = _s_Station, _d_Region, _d_Size, _s_Shape, _s_Description

                      }

 

      Explanation of Format:

 

            _s_Format = "%s %lf %lf %s %[^$]"

 

                  _s_ Format = “%s = read characters until a space is found

                  _s_Format = “%s %lf = skip the spaces, then read the number

            _s_Format = “%s %lf %lf = skip the spaces, then read the number

_s_Format = “%s %lf %lf %s = skip the spaces, then read the characters up to the next space

_s_Format = “%s %lf %lf %s %[^$]” = skip the spaces, then read all characters up to the end of the line

 

 

     Example 9:  This is an unusual example that came from a real case.

 

Sample Input File Format:

 

1001      490334.02731   378511.83746  1210.9968   CULVI/1:PIPE #1

 

Where we want the following values:

 

            _d_PointNumber = 1001

            _d_Xcoordinate = 490334.02731

            _d_Ycoordinate = 378511.83746

            _d_Elevation = 1210.9968

            _s_Description = CULV/1

            _d_LinkID = 1

 

 

            Corresponding ASCII Read Syntax:

 

                  _s_Format = "%lf %lf %lf %lf %[^:]:%[^#]#%lf"

                  while NOT (END OF FILE)

                      {

                              ASCII READ FILE = _s_file_name

                                          FORMAT = _s_Format

                                           VAR = _d_PointNumber,

                                                       _d_Xcoordinate,

                                                       _d_Ycoordinate,

                                                       _d_Elevation,

                                                       _s_Description,

                                                       _s_Junk,

                                                       _d_LinkID

                      }

 

      Explanation of Format:

 

            _s_Format = "%lf %lf %lf %lf %[^:]:%[^#]#%lf"

 

                  _s_ Format = “%lf = read the number

                  _s_Format = “%lf %lf = skip the spaces, then read the number

            _s_Format = “%lf %lf %lf = skip the spaces, then read the number

_s_Format = “%lf %lf %lf %lf = skip the spaces, then read the number

_s_Format = “%lf %lf %lf %lf %[^:] = skip the spaces, then read all characters up to the ‘:’

_s_Format =  “%lf %lf %lf %lf %[^:]: = skip the ‘:’

_s_Format =  “%lf %lf %lf %lf %[^:]: %[^#] = read all characters up to the ‘#’

_s_Format =  “%lf %lf %lf %lf %[^:]: %[^#]# = skip the ‘#’

_s_Format =  “%lf %lf %lf %lf %[^:]: %[^#]#%lf = read the number