Thats how i learned to read from files and it works fine.
How you read/write files depends on the context: what are actually wanting to achieve.
var
F: TextFile;
S: String;
begin
AssignFile(F,'test.txt');
Reset(F); //Open F for reading only
while not EOF(F) do //as long as we are not at the end of the file do:
begin
readln(F,S); //read line from file
writeln(S); //display line on the console
end;
CloseFile(F);
...
AssignFile(F,'test2.txt');
//the rewrite() wil create the file and ope it for writing.
//If the file already existed it will erase and then overwrite it's contents
Rewrite(F);
repeat
write('Enter new line (empty line quits): ');
readln(S); //read input from user
writeln(F,S); //write input from user to file
until S = ''; //until user enters an empty string
end;
First I read and display all lines in the file 'test.txt'.
For this I need a loop, since I do not know how many lines, if at all, there are in the file.
Second I create a file 'test2.txt' and ask the user what to write in it.
Again I need a loop, to give the user the opportunity to enter as few or much lines as he wants.
So in both cases I need a loop.
Now take a look at this (slightly simplified) piece of code from you:
While NOT FIleexists('Wallet.Dat') Do
begin
Writeln('You Do Not Have a wallet yet!');
writeln('Input your current wallet balance');
Readln(WalletV.Balance);
Rewrite(WalletV.WFile);
writeln(WalletV.WFile,WalletV.Balance);
Closefile(WalletV.WFile);
writeln('Your Wallet Has been saved!');
end;
Using the while loop here implies that you expect the the value of FileExists('Wallet.dat') to change to true, but you do not know when this will happen.
By itself that is not always illogical, but in this case it is.
When you do
rewrite(WalletV.WFile);
You will create the file 'Wallet.dat'. If that would not be true you have an IO Error that you must deal with (possibly the file already exists and is read-only).
Let's say the rewrite() is succesfull. Now we know that FileExists('Wallet.Dat') is True in the second evaluation of the loopcondition and the code will not be run again. And all will be fine.
But if rewrite() fails, FileExists() will be False in the second evaluation of FileExists() and you will again run the exact same code that failed. If rewrite() failed the frist time, it will also fail the second time and so on. You potentially created an endless loop here.
(Not in practice though, since the IO Error that made rewrite() fail, will also crash your program with a runtime error)
A more logical approach would be in this case:
...
if not FileExists('Wallet.Dat') then
begin
//code here to create the file and put initial content in it
//if an error occurred display it and abandon
end;
...
Also in the situation where you want to read only the first line of a text file you did (in another related thread):
...
Reset(WalletFile);
while not EOF(WalletFile) do
readln(WalletFile,S);
//some more code
end;
...
And you expected S to contain the first line in the file.
However if the file holds more than 1 line, S will have the last line in it.
If you only want to read the first line do it like this:
...
Reset(WalletFile);
// EOF might be true, and in that case we will never do a readln, so assign
//empty value to S
S := '';
if not EOF(WalletFile) do
readln(WalletFile,S);
//some more code
end;
...
end;
Can you see the difference here between the approaches that require a loop and the approaches that make more sense without a loop? And do you understand why?
Bart