Hello! So I'm generally new to programming so I have been messing around with FPC and Lazarus for the last week or so. So I have this code that is to read a text file that is encoded in UTF-16 LE and then I need to convert it to UTF-8 and then save it back out to the same file. Basically convert the file to a different encoding. My goal was to read two separate lines from the file into two separate text fields. Since I had a lot of issues mostly due to my coding skills, my current method is very bad so I wanted to find a better solution but I'm having some issue. I'm also using the newest utf8tools library by Leo. The old code is right now is this:
Fs := TCharEncStream.Create;
Fs.LoadFromFile('Start.txt');
StartupParams := Fs.UTF8Text;
Fs.Free;
AssignFile(StartupParamsFile,'Start.txt');
ReWrite(StartupParamsFile);
WriteLn(StartupParamsFile,StartupParams);
Reset(StartupParamsFile);
ReadLn(StartupParamsFile,StartupParams); //Read 1st line of file
Main.SourceDirSelect.Text := UTF8Encode(StartupParams); //1st line of file -> Source field
ReadLn(StartupParamsFile,StartupParams); //Read 2nd line of file
Main.DestinationDirSelect.Text := UTF8Encode(StartupParams); //2nd line of file -> Destination field
This works so it does what I want it to do. However I want to improve upon it further. Since utf8tools have very little documentation, I tried looking at the example but I don't think I full understand how it works. I tried to improve it by:
Fs := TCharEncStream.Create;
Fs.LoadFromFile('Start.txt');
Fs.UniStreamType := TUniStreamTypes(ufUtf8);
Fs.Free;
AssignFile(StartupParamsFile,'Start.txt');
Reset(StartupParamsFile);
ReadLn(StartupParamsFile,StartupParams); //Read 1st line of file
Main.SourceDirSelect.Text := UTF8Encode(StartupParams); //1st line of file -> Source field
ReadLn(StartupParamsFile,StartupParams); //Read 2nd line of file
Main.DestinationDirSelect.Text := UTF8Encode(StartupParams); //2nd line of file -> Destination field
and various other ways with no luck at all. It actually does nothing at all. Do you have any suggestions or how I should approach this in a good way? (And... my code is probably very bad isn't it? Not very elegant at all...)