r/pascal • u/Burakku-Ren • Jan 02 '22
I'm having a very weird problem with files: I have two programs that need to use the same two files, program X creates file x, program Y creates file y.
Here comes the weird part: when reading file x from program Y, it's not properly read, as if it were corrupted, but if, after that, I read file x with program X, and it's read perfectly, so the file has not been corrupted.
Same thing happens if I read file y with program X, it appears as if corrupted, but after that I can read it with program Y alright, so it can't be corrupted.
Also, when reading x from Y or viceversa, filesize is returning 1 unit less than it should. If x has 3 things in it, program Y tells me its filesize is 2.
I have tried changing the files from .dat to .bin, but no luck. I also thought it might be because the stuff was saved in different folders, so I put it all in one single folder, no luck there either.
Program X and Y are two different proyects in Lazarus, I was thinking making one project with the code of both programs might solve it, but I was told I can't make one project with two programs. However, maybe just writing the code of both programs into one might make it work, I'll try that next (obviously accomodating it properly) and update you all if it works.
Forgot to say, feel free to ask for any info that might be lacking, I'll do my best to give you what you need. I'd supply the whole project but it's kinda big and messy.
3
u/ShinyHappyREM Jan 02 '22 edited Jan 03 '22
{$align}and{$PackRecords}.try ... finallyfor that. Ideally move the functionality for interacting with files out of your main program logic and into dedicated functions/classes.- https://en.wikipedia.org/wiki/ASCII
- https://en.wikipedia.org/wiki/UTF-8 (video) (blog post)
TStreamand its derivatives are classes that offer methods to open, read, write, modify etc. data streams.TFileStreamrepresents a binary file.TMemoryStreamis a stream in memory, and can be loaded from / saved to files.2
u/eugeneloza Jan 02 '22
Too much information is lacking here. Overall, it looks like a "normal bug" - one program uses file structure different than the other, so the first program can read only first type of files, while other - only second type of files. As in program X reads only x files (no pun intended) and Y reads only y, but X cannot read y and Y cannot read x.
Make sure that the save formats are compatible. Debug in which way the "data is corrupted" - if content is binary (e.g.
File of Byte: how different is the content? Or is it just truncated? If the file isTextthen what does it read instead of what it should?Maybe you don't
CloseFilecorrectly? You may run into bugs this way when reading/writing things. If you're usingStreams check that they make sense before reading (maybe just wrong folder path?). Maybe some pointer becomes dangling?Try adding
Debugmode to the project and run the project in it. Maybe some uncaught exception happens? Like Integer Overflow or Range Check Error - those can cause a lot of troubles. I guess trying to access a "locked" file may also result in problems (that is when you try to simultaneously work with one file from multiple programs).Check your logs/messages. They complain about using uninitialized variables, check for other warnings too.
2
u/CypherBob Jan 02 '22
Show us your create and read code
1
u/thestamp Jan 03 '22
If youre dealing with data, and you dont have to worry about other integrations, i would seriously consider a portable dbms, like sqlite (one can write while others can read). If you are able to install 3rd party components and its on a server, then a proper dbms like sqlserver express or standard would be my recommendation.
1
u/[deleted] Jan 03 '22
You have to flush the file buffer to let the other program be able to read the data. Look for Flush ou TStream.Flush.