Recent

Author Topic: (SOLVED)Reading specific line of file  (Read 6812 times)

bigmaneds

  • New Member
  • *
  • Posts: 13
(SOLVED)Reading specific line of file
« on: April 29, 2017, 07:41:06 pm »
Hello! I have question, how can you read specific line on file? Like I have file with random lines and I want to
read for example 15th line.
I need it for my school project, i am making program which takes all the info which user gives and then stores every info in separate lines. Later code becomes a big interface which if needed opens specific line of file- specific info on call. I only know crt libaray, if you use any other library while helping me, could you please explain every step... ?
« Last Edit: April 29, 2017, 10:47:24 pm by bigmaneds »

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Reading specific line of file
« Reply #1 on: April 29, 2017, 08:52:40 pm »
Welcome back bigmaneds.
Glad to see you're coming back to learn more.

Random file access, aaaah ... it's a very old thing in my memory. When I'm first learning BASIC programming decades ago, I heard it a lot. Modern days, we use database. But I understand random file access is still important as a topic for programming teaching.

My memory is not perfect, so seniors ... correct me if I'm wrong.

I googled the web, random file access is not related with Pascal. It is a topic in BASIC programming.

The definition said:
Quote
Random files are record-based files with an internal structure that supports "direct access" by record number.

So, the similar thing in Pascal programming should be Record-type Binary Files. Basically it consists of:
- record type definition
- using keyword: file of _the_record_type_
- commands: assign, reset, rewrite, read, write, close, seek

It won't be too hard. I found a good tutorial for you (goto the topic: "Binary Files and Records"):
http://pascal-programming.info/lesson11.php#jump5

Here is a list of reference:
- Assign: http://www.freepascal.org/docs-html/rtl/system/assign.html
- Reset: http://www.freepascal.org/docs-html/rtl/system/reset.html
- Rewrite: http://www.freepascal.org/docs-html/rtl/system/rewrite.html
- Read: http://wiki.freepascal.org/Read
- Write: http://www.freepascal.org/docs-html/rtl/system/write.html
- Close: http://www.freepascal.org/docs-html/rtl/system/close.html
- Seek: http://pascal-programming.info/lesson11.php#jump5

Follow that tutorial, come back if ever have problem.

Have fun.

bigmaneds

  • New Member
  • *
  • Posts: 13
Re: Reading specific line of file
« Reply #2 on: April 29, 2017, 10:47:11 pm »
Thank you again for your help, I really appreciate it! I will try using data recording by modifying and testing codes from your given URL's.
Have a nice day, good luck with your future projects  ;)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: (SOLVED)Reading specific line of file
« Reply #3 on: April 29, 2017, 11:42:34 pm »
If you want random access for text files you have to do it the slow way - scanning every line of the text file for the desired data.
Something like the following:

Code: Pascal  [Select][+][-]
  1. program RandomAccessTextFiles;
  2.  
  3. uses sysutils;
  4.  
  5. type
  6.   TFieldType = (fName, fAge, fStreet, fTown);
  7.  
  8.   TFieldTypeCharArray = array[TFieldType] of Char;
  9.  
  10.   TDataStrings = array[TFieldType] of string;
  11.  
  12. const
  13.   FieldChars: TFieldTypeCharArray = ('!', '#', '$', '%');
  14.   TextFilename = 'data.txt';
  15.  
  16. function MakeData(aField: TFieldType; aString: String): String;
  17. begin
  18.   Result:=FieldChars[aField] + aString;
  19. end;
  20.  
  21. function GetDataFrom(const aString: String; out aFieldType: TFieldType): String;
  22. begin
  23.   Result:='';
  24.   if (Length(aString) < 3) then
  25.     Writeln('GetData error: probable blank entry in ',aString)
  26.   else case aString[1] of
  27.     '!': aFieldType:=fName;
  28.     '#': aFieldType:=fAge;
  29.     '$': aFieldType:=fStreet;
  30.     '%': aFieldType:=fTown;
  31.     else WriteLn('GetData: error in first character ',aString[1],' of ',aString);
  32.   end;
  33.   Result:=Copy(aString, 2, Length(aString));
  34. end;
  35.  
  36. procedure GetUserData(var ds: TDataStrings; var isFinished: boolean);
  37. var
  38.   s: string;
  39.   ch: Char;
  40. begin
  41.   isFinished:=False;
  42.   Write('Enter name: ');
  43.   ReadLn(s); ds[fName]:=MakeData(fName, s);
  44.   Write('Enter age: ');
  45.   ReadLn(s); ds[fAge]:=MakeData(fAge, s);
  46.   Write('Enter street: ');
  47.   ReadLn(s); ds[fStreet]:=MakeData(fStreet, s);
  48.   Write('Enter town: ');
  49.   ReadLn(s); ds[fTown]:=MakeData(fTown, s);
  50.   Write('(F)inish or (C)ontinue? ');
  51.   Read(ch);
  52.   isFinished:=(ch in ['f', 'F']);
  53.   WriteLn;
  54. end;
  55.  
  56. procedure WriteData(aDataStrings: TDataStrings; var aFile: TextFile);
  57. var
  58.   ft: TFieldType;
  59. begin
  60.   for ft in TFieldType do
  61.     WriteLn(aFile, aDataStrings[ft]);
  62. end;
  63.  
  64. procedure ShowData(const aRecordName: string);
  65. var
  66.   s, name: string;
  67.   ft: TFieldType;
  68.   aFile: TextFile;
  69.   found: Boolean=False;
  70. begin
  71.   AssignFile(aFile, TextFilename);
  72.   Reset(aFile);
  73.   while not EOF(aFile) do begin
  74.     ReadLn(aFile, s);
  75.     name:=GetDataFrom(s, ft);
  76.     if (ft = fName) and SameText(name, aRecordName) then
  77.       begin
  78.         found:=True;
  79.         WriteLn('Name: ',name);
  80.         ReadLn(aFile, s);
  81.         name:=GetDataFrom(s, ft);
  82.         if (ft = fAge) then
  83.           WriteLn('Age: ',name)
  84.         else Writeln('error');
  85.         ReadLn(aFile, s);
  86.         name:=GetDataFrom(s, ft);
  87.         if (ft = fStreet) then
  88.           WriteLn('Street: ',name)
  89.         else WriteLn('error');
  90.         ReadLn(aFile, s);
  91.         name:=GetDataFrom(s, ft);
  92.         if (ft = fTown) then
  93.           WriteLn('Town: ',name)
  94.         else WriteLn('error');
  95.       end;
  96.   end;
  97.   if not found then
  98.     Writeln('Could not find line in file with name: ',aRecordName);
  99.   CloseFile(aFile);
  100. end;
  101.  
  102. var
  103.   txtFile: TextFile;
  104.   ds: TDataStrings;
  105.   dataEntryFinished: Boolean=False;
  106.   name: string;
  107.   ch: Char;
  108.   ft: TFieldType;
  109.  
  110. begin
  111.   WriteLn('Data entry');
  112.   WriteLn('----------');
  113.  
  114.   AssignFile(txtFile, TextFilename);
  115.   Rewrite(txtFile);
  116.   repeat
  117.     for ft in TFieldType do
  118.       ds[ft]:='';
  119.     GetUserData(ds, dataEntryFinished);
  120.     WriteData(ds, txtFile);
  121.   until dataEntryFinished;
  122.   CloseFile(txtFile);
  123.  
  124.   Writeln;
  125.   WriteLn('Data retrieval');
  126.   WriteLn('--------------');
  127.   repeat
  128.     WriteLn('Whose name do you want to retrieve?');
  129.     ReadLn(name);
  130.     ShowData(name);
  131.     Write('(C)ontinue or (Q)uit? ');
  132.     ReadLn(ch);
  133.   until (ch in ['Q','q']);
  134. end.

bigmaneds

  • New Member
  • *
  • Posts: 13
Re: (SOLVED)Reading specific line of file
« Reply #4 on: May 02, 2017, 10:52:17 pm »
Sorry for late answer-was quite busy these past days. Thank you for your code, for first sight looks a bit complicated for me, but a bit more concentration and I will understand. I will take note of this code.
Thank you!  ;)

 

TinyPortal © 2005-2018