Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Reading each individual character

bubblybubbly Member Posts: 18
I know that I use readln for reading each line of the text file, what do I use for reading each individual character?
I searched for StringSplit() but couldn't find anything.

Thanks in advance,

bubbly

Comments

  • zibadianzibadian Member Posts: 6,349
    : I know that I use readln for reading each line of the text file, what do I use for reading each individual character?
    : I searched for StringSplit() but couldn't find anything.
    :
    : Thanks in advance,
    :
    : bubbly
    :
    You can read individual characters from file using Read(). A string is defined as an array[1..MaxInt] of char. So if you want to get (or set) the 1st char from a string, you can use this code:
    [code]
    MyString[1]
    [/code]
    If you want to get a part of a string, you can use Copy() if you know the location and the length. I've designed a StringSplit() function if the string consists of fields delimited by a certain char. This function can be found in the CodePedia (see links on the left hand of this page).
  • GaashiusGaashius Member Posts: 349
    : : I know that I use readln for reading each line of the text file, what do I use for reading each individual character?
    : : I searched for StringSplit() but couldn't find anything.
    : :
    : : Thanks in advance,
    : :
    : : bubbly
    : :
    : You can read individual characters from file using Read(). A string is defined as an array[1..MaxInt] of char. So if you want to get (or set) the 1st char from a string, you can use this code:
    : [code]
    : MyString[1]
    : [/code]
    : If you want to get a part of a string, you can use Copy() if you know the location and the length. I've designed a StringSplit() function if the string consists of fields delimited by a certain char. This function can be found in the CodePedia (see links on the left hand of this page).
    :
    Here are some concrete examples:
    [code]
    ...
    assignfile(stream,'somethin.txt');
    reset(stream);
    readln(str);
    indchar:=str[1];
    closefile(stream);
    ...
    [/code]

    [code]
    ...
    assignfile(stream,'somethin.txt');
    reset(stream);
    read(indchar);
    closefile(stream);
    ...
    [/code]
    In the examples above the str is a string variable, and indchar is a char.

    **************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)


  • bubblybubbly Member Posts: 18
    [b][red]This message was edited by bubbly at 2005-4-12 6:50:15[/red][/b][hr]
    : : : I know that I use readln for reading each line of the text file, what do I use for reading each individual character?
    : : : I searched for StringSplit() but couldn't find anything.
    : : :
    : : : Thanks in advance,
    : : :
    : : : bubbly
    : : :
    : : You can read individual characters from file using Read(). A string is defined as an array[1..MaxInt] of char. So if you want to get (or set) the 1st char from a string, you can use this code:
    : : [code]
    : : MyString[1]
    : : [/code]
    : : If you want to get a part of a string, you can use Copy() if you know the location and the length. I've designed a StringSplit() function if the string consists of fields delimited by a certain char. This function can be found in the CodePedia (see links on the left hand of this page).
    : :
    : Here are some concrete examples:
    : [code]
    : ...
    : assignfile(stream,'somethin.txt');
    : reset(stream);
    : readln(str);
    : indchar:=str[1];
    : closefile(stream);
    : ...
    : [/code]
    :
    : [code]
    : ...
    : assignfile(stream,'somethin.txt');
    : reset(stream);
    : read(indchar);
    : closefile(stream);
    : ...
    : [/code]
    : In the examples above the str is a string variable, and indchar is a char.
    :
    : **************
    : Any questions? Just ask!
    :
    : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    :
    :
    :

    I'm still tryin to get this part to work. At the moment, I have two types of loops and neither of them will work for me. I have...


    While NOT EOF(TableStructure) Do
    Begin
    Readln(TableStructure,S);
    For i:=1 to length(S) do
    Begin

    While S[i] <> TAB do {This is the first loop I tried}
    Header := Header + S[i];

    Repeat Header := (Header + S[i]) {This is the 2nd loop I tried}
    Until S[i] = TAB;

    end;

    It's just going into an infinite loop. If I put in an If Statement it will work, but it's not what I need. If copies everything except the TAB, I only need from Position[0] to TAB.



  • zibadianzibadian Member Posts: 6,349
    : [b][red]This message was edited by bubbly at 2005-4-12 6:50:15[/red][/b][hr]
    : : : : I know that I use readln for reading each line of the text file, what do I use for reading each individual character?
    : : : : I searched for StringSplit() but couldn't find anything.
    : : : :
    : : : : Thanks in advance,
    : : : :
    : : : : bubbly
    : : : :
    : : : You can read individual characters from file using Read(). A string is defined as an array[1..MaxInt] of char. So if you want to get (or set) the 1st char from a string, you can use this code:
    : : : [code]
    : : : MyString[1]
    : : : [/code]
    : : : If you want to get a part of a string, you can use Copy() if you know the location and the length. I've designed a StringSplit() function if the string consists of fields delimited by a certain char. This function can be found in the CodePedia (see links on the left hand of this page).
    : : :
    : : Here are some concrete examples:
    : : [code]
    : : ...
    : : assignfile(stream,'somethin.txt');
    : : reset(stream);
    : : readln(str);
    : : indchar:=str[1];
    : : closefile(stream);
    : : ...
    : : [/code]
    : :
    : : [code]
    : : ...
    : : assignfile(stream,'somethin.txt');
    : : reset(stream);
    : : read(indchar);
    : : closefile(stream);
    : : ...
    : : [/code]
    : : In the examples above the str is a string variable, and indchar is a char.
    : :
    : : **************
    : : Any questions? Just ask!
    : :
    : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : :
    : :
    : :
    :
    : I'm still tryin to get this part to work. At the moment, I have two types of loops and neither of them will work for me. I have...
    :
    :
    : While NOT EOF(TableStructure) Do
    : Begin
    : Readln(TableStructure,S);
    : For i:=1 to length(S) do
    : Begin
    :
    : While S[i] <> TAB do {This is the first loop I tried}
    : Header := Header + S[i];
    :
    : Repeat Header := (Header + S[i]) {This is the 2nd loop I tried}
    : Until S[i] = TAB;
    :
    : end;
    :
    : It's just going into an infinite loop. If I put in an If Statement it will work, but it's not what I need. If copies everything except the TAB, I only need from Position[0] to TAB.
    :
    :
    You forgot to increment the i with each turn of the while or repeat loop. Both loops will produce an infinite string with only the first character of s.
    If you only want to have the first part until the tab, I would suggest that you use the Copy() and Pos() functions. Here is a workable code:
    [code]
    Header := Copy(s, 1, Pos(Tab, s)-1));
    [/code]
    See help files for more info on Copy() and Pos().
    Note: if you want to have more control than this, download the StringSplit() function from the CodePedia.
Sign In or Register to comment.