Recent

Author Topic: [SOLVED]Reading Just A Few Lines of a CSV File in Pascal  (Read 5634 times)

jufau

  • New Member
  • *
  • Posts: 42
[SOLVED]Reading Just A Few Lines of a CSV File in Pascal
« on: August 31, 2015, 03:15:49 pm »
Hi All,
I would like to ask your help for the following question:
I have some huge CSV/TXT files (over 1.5GB of size each), and I would like to load just a few lines of such files. The reason is because I need to link the field name of the file to my principal database field name. So I don't really need to load the entire file (which can take a while) in the memory. Only a few lines would be enough. Some thing like that
ID;NAME;ADDRESS;CITY
1;JOSH;1 MAIN STREET;BOSTON
2;EMILY;1 MAIN STREET;BOSTON
3;BILL;1 MAIN STREET;BOSTON

The above option will give me exactly what I need to know, which is load the field name(ID,NAME,ADDRESS, CITY) and just a few records for reference. Not the entire (1.5GB) file.

So how could I accomplish such thing?

thanks in advance.
J
« Last Edit: September 01, 2015, 07:40:11 pm by jufau »

derek.john.evans

  • Guest
Re: READING JUST A FEW LINES OF A CSV FILE IN PASCAL
« Reply #1 on: August 31, 2015, 03:29:23 pm »
Sounds like they need to be loaded into database tables. Its either that or, write your own code to pre-calcutate index files which would contain line positions sorted by each field you want to link to.

Which, is, after all why DB's were created. So, we dont have to write such code.

*EDIT*. But, if you are just wanting to load a few lines from the start, the standard Pascal file functions should do.

http://wiki.freepascal.org/File_Handling_In_Pascal
« Last Edit: August 31, 2015, 03:33:03 pm by derek.john.evans »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: READING JUST A FEW LINES OF A CSV FILE IN PASCAL
« Reply #2 on: August 31, 2015, 03:34:56 pm »
Code: [Select]
Just with basic textfile I/O using assignfile/reset and readln?

var f : textfile;
   s : string;
  nr: integer;
begin
  assignfile(f,'filename.txt');
  reset(f);
 nr:=5;
 while not EOF(F) and (nr>0) do
     begin
        readln(f,s);
        memo1.lines.add(s);
        dec(nr);
   end;
 closefile(f);
    end;


eny

  • Hero Member
  • *****
  • Posts: 1634
Re: READING JUST A FEW LINES OF A CSV FILE IN PASCAL
« Reply #3 on: August 31, 2015, 06:18:09 pm »
IT IS NOT FORBIDDEN TO USE LOWERCASE LETTERS IN A TOPIC TITLE
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: READING JUST A FEW LINES OF A CSV FILE IN PASCAL
« Reply #4 on: August 31, 2015, 09:47:15 pm »
IT IS NOT FORBIDDEN TO USE LOWERCASE LETTERS IN A TOPIC TITLE

lol - at least the poster didn't use all caps in the body of the post...
Lazarus Ver 2.2.6 FPC Ver 3.2.2
Windows 10 Pro 64-bit

jufau

  • New Member
  • *
  • Posts: 42
Re: Reading Just A Few Lines of a CSV File in Pascal
« Reply #5 on: September 01, 2015, 05:20:57 pm »
:(
Sorry guys for the Upper Case.  My bad.

Changed
 :D

J

jufau

  • New Member
  • *
  • Posts: 42
[SOLVED]Re: Reading Just A Few Lines of a CSV File in Pascal
« Reply #6 on: September 01, 2015, 07:39:39 pm »
Thank you "marcov"
It was exactly what I needed. You helped me a lot

thank you all guys for replying my messages as well. As always, my questions are answered in no time.

Great forum, great people

J

 

TinyPortal © 2005-2018