Board index » delphi » StrToChar

StrToChar

Hi there!
I want to store a string in a file as char?

Pe.: The user typed the string 2000 in a Tedit object.
I want to take Tedit1.text and store it in a file as char.

How should I define this field in the file?

How can I assign this string to this char field without having 'incompatible
type' error?

 

Re:StrToChar


Quote
Guillermo Sanchez wrote:
> I want to store a string in a file as char?

> Pe.: The user typed the string 2000 in a Tedit object.
> I want to take Tedit1.text and store it in a file as char.

A char is one letter.
You want to store "2000" as one letter?

--
                                                   Andreas
if ((sex==fem)&&(hobby==comp)&&(age~25)&&(status==single)
&&(cntry=GER)) goto http://www.kochandreas.com/home/single.htm

Re:StrToChar


Hi,

Quote
>I want to store a string in a file as char?

How. A string can have manny characters. You mean an array of chars?

Quote
>Pe.: The user typed the string 2000 in a Tedit object.
>I want to take Tedit1.text and store it in a file as char.

Ditto.

Quote
>How should I define this field in the file?

Using Paradox, DBase, or what? Probably a text field is your best option,
anyway.

Quote
>How can I assign this string to this char field without
>having 'incompatible type' error?

Delphi offers manny ways of transforming a Pascal string into a PChar (null
terminated) string, if that's what you want. Search Delphi's help for
'String handling routines' and 'String handling routines (null
terminated)'. You can also use typecasting. But if you use a text field,
you'll not need that.

Humberto Jemma (Brazil)

-------------------------------
Message Posted by  <dedGateway>
http://www.dedonline.com/forumb

Re:StrToChar


Sorry not being more specific.
I meant an Array of char.
I'm going to check 'String handling routines' as Humberto told me... I'll
let you all know, thanks a lot.

Quote
"Andreas Koch" <m...@kochandreas.com> wrote in message

news:afa99d$n0f$03$1@news.t-online.com...
Quote

> Guillermo Sanchez wrote:

> > I want to store a string in a file as char?

> > Pe.: The user typed the string 2000 in a Tedit object.
> > I want to take Tedit1.text and store it in a file as char.

> A char is one letter.
> You want to store "2000" as one letter?

> --
>                                                    Andreas
> if ((sex==fem)&&(hobby==comp)&&(age~25)&&(status==single)
> &&(cntry=GER)) goto http://www.kochandreas.com/home/single.htm

Re:StrToChar


Quote
In article <3d18889b$1_2@dnews>, Guillermo Sanchez wrote:
> I want to store a string in a file as char?

?? Makes no sense. A Char is a single byte in size, so you cannot cram a
string of length > 1 into it, at least if you want to be able to restore
the string again <g>.

Quote
> The user typed the string 2000 in a Tedit object.
> I want to take Tedit1.text and store it in a file as char.

What kind of file so you want? A textfile (look at Assignfile, Textfile,
Rewrite, WriteLn in the online help)? A file of some record type (in which
case the record fields need to be declared as String[someLength], where
someLength <= 255)? A database file?

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:StrToChar


I meant an array of char, sorry by that.
Can I?
"Peter Below (TeamB)" <100113.1...@compuXXserve.com> wrote in message
news:VA.00008d91.0091a0ad@antispam.compuserve.com...
Quote
> In article <3d18889b$1_2@dnews>, Guillermo Sanchez wrote:
> > I want to store a string in a file as char?

> ?? Makes no sense. A Char is a single byte in size, so you cannot cram a
> string of length > 1 into it, at least if you want to be able to restore
> the string again <g>.

> > The user typed the string 2000 in a Tedit object.
> > I want to take Tedit1.text and store it in a file as char.

> What kind of file so you want? A textfile (look at Assignfile, Textfile,
> Rewrite, WriteLn in the online help)? A file of some record type (in which
> case the record fields need to be declared as String[someLength], where
> someLength <= 255)? A database file?

> --
> Peter Below (TeamB)
> Use the newsgroup archives :
> http://www.mers.com/searchsite.html
> http://www.tamaracka.com/search.htm
> http://groups.google.com
> http://www.prolix.be

Re:StrToChar


Peter:
Here is the whole thing.....

WHAT IS IT ALL ABOUT?
I'm reading a text file with BlockRead.  This file has an structure wich I
defined using type .... record.
Some of the fields are array of char, others are longint, byte and the rest
are smallint.
I'm reading this file without any problem.

WHAT DO I NEED?
But I need to write a NEW text file with a different file structure.

WHAT HAVE I DONE?
I've defined the file structure of the NEW text file the same way using type
... record.  So I can assign
the fields of source file to the corresponding target file fields.  Take in
count not all the corresponding
fields have the same data type in source and target.

THERE ARE 3 ISSUES I CURRENTLY HAVE:
One field in source file is stored as longint and I have to write it in the
new file as an array of char.
Another field in source file is stored as smallint and I have to write it in
the new file as an array of char.
Another field in source file is stored as byte and I have to write it in the
new file as char.

SOME CODE...:
What I though was to use inttostr( ), and do this:

        for i := 1 to 4 do
        begin
          ToFile.arrayofcharfieldA [i] :=
copy(inttostr(FromFile.longintfield),i,1);
        end;

        for i := 1 to 2 do
        begin
          ToFile.arrayofcharfieldB [i] :=
copy(inttostr(FromFile.smallintfield),i,1);
        end;

        layout.charfieldC := bytefield;

BUT, I'm getting 'imcompatible type' error.

Can you help me out with this?
Best regards,
Guillermo

Quote
"Guillermo Sanchez" <guillermo_sanc...@codetel.com.do> wrote in message

news:3d18c269$1_2@dnews...
Quote
> I meant an array of char, sorry by that.
> Can I?
> "Peter Below (TeamB)" <100113.1...@compuXXserve.com> wrote in message
> news:VA.00008d91.0091a0ad@antispam.compuserve.com...
> > In article <3d18889b$1_2@dnews>, Guillermo Sanchez wrote:
> > > I want to store a string in a file as char?

> > ?? Makes no sense. A Char is a single byte in size, so you cannot cram a
> > string of length > 1 into it, at least if you want to be able to restore
> > the string again <g>.

> > > The user typed the string 2000 in a Tedit object.
> > > I want to take Tedit1.text and store it in a file as char.

> > What kind of file so you want? A textfile (look at Assignfile, Textfile,
> > Rewrite, WriteLn in the online help)? A file of some record type (in
which
> > case the record fields need to be declared as String[someLength], where
> > someLength <= 255)? A database file?

> > --
> > Peter Below (TeamB)
> > Use the newsgroup archives :
> > http://www.mers.com/searchsite.html
> > http://www.tamaracka.com/search.htm
> > http://groups.google.com
> > http://www.prolix.be

Re:StrToChar


Quote
Guillermo Sanchez wrote:
> SOME CODE...:
> What I though was to use inttostr( ), and do this:

>         for i := 1 to 4 do
>         begin
>           ToFile.arrayofcharfieldA [i] :=
> copy(inttostr(FromFile.longintfield),i,1);
>         end;

>         for i := 1 to 2 do
>         begin
>           ToFile.arrayofcharfieldB [i] :=
> copy(inttostr(FromFile.smallintfield),i,1);
>         end;

>         layout.charfieldC := bytefield;

> BUT, I'm getting 'imcompatible type' error.

> Can you help me out with this?

Thats easy. Copy(s,i,1) returns a string
of length 1, not a char.
To get a char, use s[i]:

var
  s:string;

          for i := 1 to 2 do
          begin
          s:=inttostr(FromFile.smallintfield);
            ToFile.arrayofcharfieldB [i] := s[i];
          end;

--
                                                   Andreas
if ((sex==fem)&&(hobby==comp)&&(age~25)&&(status==single)
&&(cntry=GER)) goto http://www.kochandreas.com/home/single.htm

Re:StrToChar


"Guillermo Sanchez" <guillermo_sanc...@codetel.com.do> wrote

Quote
> I want to store a string in a file as char?
> Pe.: The user typed the string 2000 in a Tedit object.
> I want to take Tedit1.text and store it in a file as char.

Guillermo, Why don't you use the simple AssignFile, ReWrite, WriteLn,
and CloseFile procedures?  --Regards, JohnH

Re:StrToChar


Quote
In article <3d18c798$1_2@dnews>, Guillermo Sanchez wrote:
> Here is the whole thing.....

Well, i appreciate that you try to give the relevant data. Unfortunately you
still did not show one important pieve of info: a part of your record
declaration that show how you have actually declared the "array of char"
fields. It matters whether you declared them with a 0 or a 1 as lower bound.
If you used a lower bound of 0 you can copy string data to the field by using
StrPLCopy or StrLCopy. If you used 1 (which I actually prefer in records since
1-based char arrays are traditionally fixed-length strings in Pascal) you have
to use something like this:

Procedure AssignFixedString( Var FixedStr: Array of Char; Const S: String );
Var
  maxlen: Integer;
Begin
  maxlen := Succ( High( FixedStr ) - Low( FixedStr ));
  FillChar( FixedStr, maxlen, ' ' ); { blank fixed string }
  If Length(S) > maxlen Then
    Move( S[1], FixedStr, maxlen )
  Else
    Move( S[1], FixedStr, Length(S));
End;

  AssignFixedString(
     ToFile.arrayofcharfieldA,
     inttostr(FromFile.longintfield));
  AssignFixedString(
     ToFile.arrayofcharfieldB,
     inttostr(FromFile.smallintfield));

The procedure will also work with 0-based arrays of char, by the way. The main
difference between using it vs. StrPLCopy is that it will fill the array with
spaces while StrPLCopy will add a #0 terminator to the copy (*in addition to
the characters copied*). That is usually not what you want if the resulting
file should be readable with a text editor.

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:StrToChar


Peter,  That you are a paragon of patience does
not go unappreciated!  Best regards, JohnH

Re:StrToChar


Jhon/Peter: Sorry if I make some of you unconfortable in any way.

Andreas, thanks, I follow your hint, and it worked fine.

Thanks to all of you anyway.  I appreciate very much you help.

Quote
"John Herbster" <Jo...@petronworld.com> wrote in message

news:3d1a33e8$1_2@dnews...
Quote
> Peter,  That you are a paragon of patience does
> not go unappreciated!  Best regards, JohnH

Other Threads