Menu

Single character input from a text file

Anonymous
2016-12-05
2016-12-05
  • Anonymous

    Anonymous - 2016-12-05

    Hello
    I have a delimiterless text file consisting of a long (~1000 chrs) set of ASCII characters (ABCDEFGHIJK ....) and I would like to read in and process each character individually. Can anyone help me with an easy way to do this, please?
    Many thanks

     
  • Rob Hagemans

    Rob Hagemans - 2016-12-05

    The easiest way: if you need to read the characters in order, you could use INPUT$ to read single characters from a text file, something like:

    10 OPEN "MYFILE" FOR INPUT AS 1
    20 FOR I = 1 TO 1000
    30   A$ = INPUT$(1,#1)
    40   PRINT A$;
    50 NEXT
    

    If you're going to have to jump around in the file, you could open as a random-access file with one-byte record length, e.g.:

    10 OPEN "MYFILE" FOR RANDOM AS 1 LEN=1
    15 FIELD#1, 1 AS A$
    20 FOR I = 1 TO 1000
    30   GET#1, I
    40   PRINT A$;
    50 NEXT
    
     

    Last edit: Rob Hagemans 2016-12-05
  • Anonymous

    Anonymous - 2016-12-05

    Hello Rob. Many thanks for your speedy response. I must confess that I had wondered about your first way, but discarded it because of the documentation entry about INPUT$ :

    INPUT$
    chars = INPUT[ ]$ (num_chars [, [#] file_num])

    Returns a string of num_chars characters from the keyboard or, if file_num is provided, from a text file.

    Parameters
    • num_chars is a numeric expression in [1—255].

    My reason for discarding the first way was that I reasoned that when I = 1000, line 30 tries to read
    num_chars (1000) into A$ , which is prohibited by the parameter definition. Have I missed something here?
    Thanks for the second process.
    Sincerely

     
  • Rob Hagemans

    Rob Hagemans - 2016-12-05

    You're right, I made a typo in the first bit of code: num_chars should be 1, not I: Each time, one character is read. The limitation is that you can read at most 255 characters at a time, but more won't fit in a string anyway.

    I've corrected the code above.

     
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.