Importing text file into a TEdit lazarus / 31-LCL ---------------------------------------------------------------------- nidrax · 2011-10-26 17:30 Hi, I have problem importing a txt file into a TEdit. I have created a one-lined text file with a string: '1234' When I first imported it, it was composed by two quotation marks and only the first letter of the line (??1). I got rid of those '?', but there's still a problem, because it imports only the first char of the string ('1' instead of '1234'). The code is: Code: [Select]procedure TForm1.MenuFileImportClick(Sender: TObject); begin   if OpenDialog1.Execute then   ShowMessage(OpenDialog1.FileName);   try     System.Assign(Worktext, OpenDialog1.FileName);     reset(Worktext);     Readln(Worktext,line);     Delete(line, 1, 2);     InputEd.Text:=(line);     System.Close(Worktext);   except     ShowMessage('Syntax error or text too long.'+#13#10+'No value were imported.')   end; end; There are declared variables: Code: [Select]var   Form1: TForm1;   Worktext: TextFile;   Line: String; Any ideas about how to fix it? ---------------------------------------------------------------------- typo · 2011-10-26 17:49 Code: [Select]    System.Assign(f, FileName);     reset(f);     Readln(f,line);     edit1.Text:= Trim(line);     System.Close(f); ---------------------------------------------------------------------- christensen · 2011-10-26 17:53 Code: [Select]var   F: TextFile;   S: string; begin   if FileExists('C:\test.txt') then begin     AssignFile(F, 'C:\test.txt');     Reset(F);     ReadLn(F, S);     Edit1.Text := S;     CloseFile(F);   end   else     ShowMessage('File C:\test.txt not found'); end; ---------------------------------------------------------------------- nidrax · 2011-10-26 18:02 Quote from: typo on October 26, 2011, 07:49:07 pmCode: [Select]    System.Assign(f, FileName);     reset(f);     Readln(f,line);     edit1.Text:= Trim(line);     System.Close(f); Still the same as mine. The difference is that I get the File Name directly from the TOpenDialog (that Trim(line) you placed makes no difference, the effect is still the same). @up Still the same as mine, minus the fact you pre-defined the text directory, that has no use for me, as I want the user to have the option of choosing any text file he likes to import. And even using your code, the effect remains the same. ---------------------------------------------------------------------- mas steindorff · 2011-10-26 18:15 it looks like your after just the first line here but I suspect you'll want the whole file later on so here is an alt. Code: [Select]var tstr :tstringlist;  <- can be defined in your form so it stays alive if you need begin  ... use your opendiag...  tstr := tstringlist.create();  tstr.loadfromfile(OpenDialog1.FileName)  // the file has been loaded into the tstring object so you can work with it. // if you used a memo, then memo1.lines.loadfromfile() will be all you need to show the full file.  (also a nice debug output)  if tstr.count > 0 then    edit.text := tstr.string[0];  //<- or any other line you wish ...  tstr.free;   // <- or place in your form close/destroy call end; ---------------------------------------------------------------------- nidrax · 2011-10-26 18:22 Quote from: mas steindorff on October 26, 2011, 08:15:00 pmit looks like your after just the first line here but I suspect you'll want the whole file later on so here is an alt. Well... The file has only one line, but program doesn't show it in its entirety. I'll try your code anyway. @edit Still the same. I got two quotation marks and the first char. When i try to load the second line, the program crashes (because it doesn't exist). @edit Nope, still the same, and when trying to load the line [1]program crashes, because it doesn't exist. @edit2 I give you the sorce, maybe you could find the error (most of main functions are not written, so don't pay attention to things not related to the import procedure): http://www.mediafire.com/?6k6t9p31t8ak6qo ---------------------------------------------------------------------- User137 · 2011-10-26 22:45 There is something weird in the format of your text file. Test with this i upload. How did you save the test.txt, what application? Opening it with hex- editor i get this kinds: HEX: FF FE 31 00 32 00 33 00 34 00 Windows Notepad makes contents of simple 1234 line (even without line change char): HEX: 31 32 33 34 ---------------------------------------------------------------------- mas steindorff · 2011-10-27 00:22 your text file has the bytes FF FE 31 00 32 00 33 00 34 00 which is in ASCII = ?   ?  1   ?   2   ?   3    ?  4  ? the ? are non printable characters.  this is the whole file. You may be using " wide string " code to create so the 0 between the numbers may be ok but the line should end with a CR = 0x0d char to be called text the frist two characters are anyones guess as to what they are but not text as I know them. ---------------------------------------------------------------------- nidrax · 2011-10-27 05:12 Yes, that was something with the text file. I just have taken one of readmes and edited it, and didn't notice that it is formatted in UCS-2. Anyway, thanks, cause I don't think I would got to that so fast. Blame and shame