Recent

Author Topic: How to find files (SOLVED)  (Read 3855 times)

bigmaneds

  • New Member
  • *
  • Posts: 13
How to find files (SOLVED)
« on: April 26, 2017, 10:49:48 pm »
Hello! I have a problem ,which I can't solve with my begginer knowledge. I made a small registration code, where at the beggining there is a question 'Are you registered', if user chooses No, then he has to create username and the code creates file with his username and the second option(problematic option), if user chooses that he is already registered, then he types inside his already created username. The problem is , I want my code to find and open the file, which is equal to his typed existing username.

Code: Pascal  [Select][+][-]
  1. program project1 ;
  2. uses crt, sysutils;
  3. var reg,lietv,eksistlietfails,fails1,fails2:string;
  4.  
  5.  
  6.  
  7.   fl:text;
  8.  
  9.  
  10.  
  11.  
  12.  
  13. begin
  14.   TextColor(Yellow);
  15.  
  16.   repeat
  17.    Writeln('Are you registered? Type "Y" or "N"');
  18.   readln(reg);
  19.  
  20.  
  21.    if (reg='N') or (reg='n') then
  22.    begin
  23.   writeln('Create your username');
  24.   readln(lietv);
  25.   fails1:=lietv; fails2:='.txt';
  26.   fails:=fails1+fails2;
  27.   assign(fl,fails);
  28.   rewrite(fl);
  29.   close(fl);
  30.   writeln('From this moment, your username will be : ',lietv);
  31.   end
  32.     else if (reg='Y') or (reg='y') then
  33.   begin
  34.  
  35.     writeln('Write your already existing username');
  36.      readln(eksistliet);
  37.  
  38.   end;
  39.  
  40.  
  41.  
  42.   until (reg='Y') or (reg='y') or (reg='N') or (reg='n');
  43.  
sorry for the messy code...  :-[
« Last Edit: April 27, 2017, 08:45:28 pm by bigmaneds »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to find files
« Reply #1 on: April 26, 2017, 11:13:37 pm »
I want my code to find and open the file, which is equal to[/i] his typed existing username
I think you meant locate, not find.

Finding a file on an entire hard-disk takes too much time. Instead make sure you know where exactly your data is stored.

You can use the FileExists function from unit sysutils to check whether or not a file is present and then simply open the file.

That is, unless i misunderstood your problem.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: How to find files
« Reply #2 on: April 26, 2017, 11:19:15 pm »
Something like this?

Code: [Select]
  assign(fl, eksistliet+'.txt');
  reset(fl); //open for reading
  //read whatever you need from that file
  close(fl);

Bart

bigmaneds

  • New Member
  • *
  • Posts: 13
Re: How to find files
« Reply #3 on: April 27, 2017, 06:59:05 am »
Well, I know how to read a certain file. I have created many files, but I need to open file which is equal to users typed existing username. Like the username is the file name, and user has to open it -There are many files created with "fl" variable a,b,c,d,e .e.c but the user types his username and opens his private file...It is hard for me to explain in english, which is not my main language.
« Last Edit: April 27, 2017, 07:02:17 am by bigmaneds »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: How to find files
« Reply #4 on: April 27, 2017, 07:43:16 am »
How about this code:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   Crt, sysutils;
  5.  
  6. var
  7.   DataLocation: string;
  8.   UserName: string;
  9.   Reg: string;
  10.  
  11. begin
  12.  
  13.   // Data initialization
  14.   DataLocation := GetCurrentDir;
  15.  
  16.   // Registered or new user?
  17.   repeat
  18.     WriteLn('Are you registred? Type "Y" or "N":');
  19.     ReadLn(Reg);
  20.     Reg := UpperCase(Reg);
  21.   until (Reg = 'Y') or (Reg = 'N');
  22.  
  23.   // Check if new username is available
  24.   if (Reg = 'N') then
  25.   begin
  26.     WriteLn('Create your username:');
  27.     ReadLn(UserName);
  28.     if FileExists(DataLocation + PathDelim + UserName + '.txt') then
  29.     begin
  30.       WriteLn('Sorry, the username has ben used.');
  31.       WriteLn('Press any key to stop this program.');
  32.       ReadKey;
  33.       Halt;
  34.     end;
  35.   end;
  36.  
  37.   // Check if the registered username is valid
  38.   if (Reg = 'Y') then
  39.   begin
  40.     WriteLn('Write your already existing username:');
  41.     ReadLn(UserName);
  42.     if not(FileExists(DataLocation + PathDelim + UserName + '.txt')) then
  43.     begin
  44.       WriteLn('Sorry, the username is not valid.');
  45.       WriteLn('Press any key to stop this program.');
  46.       ReadKey;
  47.       Halt;
  48.     end;
  49.   end;
  50.  
  51.   // do something here
  52.  
  53. end.
« Last Edit: April 27, 2017, 07:45:02 am by Handoko »

bigmaneds

  • New Member
  • *
  • Posts: 13
Re: How to find files
« Reply #5 on: April 27, 2017, 08:45:14 pm »
Thank you! You are master of your job  ;)

 

TinyPortal © 2005-2018