×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Export .dat to txt. Help

Export .dat to txt. Help

Export .dat to txt. Help

(OP)
Hello,

I would like some help. I have I customer with a program in PAscal or Tpascal, i do not have access to the code that saves data of the tables in DAT format.
I am able to read the .dat files on notepad, and get within the sistem the type and dimensions of the tables, some
tables export to txt and others do not.
The problem is that I need to export to txt the tables.
I am absolut green on Pascal.
I´ve read the www.tek-tips.comqid=1369943, that is similar but he had access to the code.
How can i export to txt reading a .dat table with the next structure (NAME,type,size)


Nregistro,word,    5
Classe,string,15
Ccusto_red,string,10
Valor,real,18
Ccusto_ext,string,15
Def1,char,1



I have installed the FPC and tried to figure out how to make the export work, as the number of records is big, how do i define the types, and the arrays....

var
  filegegevens: FILE OF tabel;  ?????
  recgegevens: tabel;
  output_text: Text;
  i: integer;
begin
  ASSIGN(filegegevens,'c:\temp\data-log.dta');
{$I-}  RESET(filegegevens) {$I+};
  ASSIGN(output_text, 'c:\temp\data-log.txt');
  rewrite(output_text);
  read(filegegevens, recgegevens);
  with recgegevens do
    begin
      writeln(output_text, Nregistro);
      writeln(output_text, Classe);
      writeln(output_text, CCusto);
      writeln(output_text, Valor);
      writeln(output_text, Def1);
         end;
  close(filegegevens);
  close(output_text);
end.

Thanks in advance
 

RE: Export .dat to txt. Help

Hi

CODE

type tabel=record
    Nregistro:Word;
    Classe:string[15];
    Ccusto_red:string[10];
    Valor:Real;
    Ccusto_ext:string[15];
    Def1:Char;
  end;

var
  filegegevens: file of tabel;
  recgegevens: tabel;
  output_text: Text;
  i: Integer;

begin

  Assign(filegegevens, 'c:\temp\data-log.dta');
  Reset(filegegevens)
  Assign(output_text, 'c:\temp\data-log.txt');
  Rewrite(output_text);

  while not Eof(filegegevens) do begin
    Read(filegegevens, recgegevens);
    with recgegevens do begin
      Writeln(output_text, Nregistro);
      Writeln(output_text, Classe);
      Writeln(output_text, CCusto_red);
      Writeln(output_text, Valor);
      Writeln(output_text, CCusto_ext);
      Writeln(output_text, Def1);
    end;
  end;

  Close(filegegevens);
  Close(output_text);

end.
 

Feherke.
http://free.rootshell.be/~feherke/

RE: Export .dat to txt. Help

(OP)
Hi Feherke,

wooooooohh Looks   great....

I am going to try it and give you a feedback..

Thank you
 

RE: Export .dat to txt. Help

(OP)
Feherke,


It worked........(just needed a ";"...!!!!)

Thank you very much....

Just one last thing how do I concatenate several exports...

Is it just appending them as the example or is better creating a program and calling the subroutines...??

Sergio

 

RE: Export .dat to txt. Help

(OP)
Hello again,

It is working, compiles OK, giving no error.

But nothing is outputed....

Gives error 3 on exiting the program...

I am sorry is probably something basic, but how to output and create the exe file, to execute elsewhere...

Or a on line place to check up the pascal basics and not take your time..

Thanks in advance...
 

RE: Export .dat to txt. Help

Hi

Quote (Sergio):

(just needed a ";"...!!!!)
Oops. I missed that after removing the compiler directives. ( Only disable the input/output checking if will handle the errors yourself programmatically. )

Quote (Sergio):

Just one last thing how do I concatenate several exports...
You mean, to run the program several times and new content should be appended to c:\temp\data-log.txt instead of overwriting the initial content ? Just replace the Rewrite procedure with Append. But because Append fails if the file not exists, you will still need Rewrite, but controlled by a condition :

CODE --> instead of Rewrite(output_text);

if FileExists('c:\temp\data-log.txt')
then Append(output_text)
else Rewrite(output_text);
To have the FileExists function available add this line to the beginning of the source code :

CODE --> above type tabel=record

uses sysutils;

Quote (Sergio):

Gives error 3 on exiting the program...
I/O error 3 means "Path not found". Check the path used in then Assign procedures.
 

Feherke.
http://free.rootshell.be/~feherke/

RE: Export .dat to txt. Help

(OP)
Hi Feherke,
Thank you again, still stuck.
Close but not yet there. Outputs the file(erro =0), but is no good data.

The name,tipe,size are:

Nregistro    word    5
codigo    string    15
codigoreducido    string    6
descricao    string    30
tipoclase    char    1
NatClase    char    1
CtaContabil    String    15
LancCCustos    Char    1
CtaContabil2    STRING    15

The code that works (COMPILE&MAKE&BUILD =OK)


CODE

Program ExpClas;
uses sysutils;

type tabel=record
    Nregistro: Word;
    Codigo:string[15];
    Ccusto_red:string[6];
    Descricao:string[30];
    TipoClase:Char;
    NatClase:Char;
    CtaContabil:String[15];
   LanCcusto:char;
    CtaContabil2:String[15];

  end;

var
  filegegevens: file of tabel;
  recgegevens: tabel;
  output_text: text;
 { i: Integer; }


  begin

  Assign(filegegevens, 'C:\FPC\..\CLASSES.DTA');
  Reset(filegegevens);
  Assign(output_text, 'C:\FPC\...\saidtxt.txt');
  Rewrite(output_text);

  while not Eof(filegegevens) do begin
    Read(filegegevens, recgegevens);
    with recgegevens do begin
      Writeln(output_text, Nregistro);
      Writeln(output_text, Codigo);
      Writeln(output_text, CCusto_red);
      Writeln(output_text, Descricao);
      Writeln(output_text, TipoClase);
      Writeln(output_text, NatClase );
      Writeln(output_text, CtaContabil);
      Writeln(output_text, LanCcusto);
      Writeln(output_text, CtaContabil2);

    end;
  end;

  Close(filegegevens);
  Close(output_text);

   end.
When the exe is run gives error = 0, and outputs the txt not readable file.Data still in binary and no sense
Am attaching the CLASSES.dta file,and how it should look like (as exportd from the program txt)
I am using for the case a table that the sistem exports to check out the procedure for then use it with he one that the original program does not export.

HELP for
1.- Make the output be correct
2.- How to export csv instead

RE: Export .dat to txt. Help

Hi

Note that Tek-Tips does not upload files. You have to upload them somewhere then paste the URL into the Attachment field in the reply form.

In meantime one thing. Maybe FreePascal requires the new TextFile keyword fro declaring text file variables. ( In old Pascal times the keyword was Text, changed later into TextFile, but I am not sure which compiler requires which. )

CODE --> instead of output_text: text;

output_text: TextFile;

Feherke.
http://free.rootshell.be/~feherke/

RE: Export .dat to txt. Help

Hi

Quote (Sergio):

Nregistro    word    5
That is not a Word. Word is stored on 2 bytes. Even more, I know about no programming language where any integer type is stored on 5 bytes. I will handle it as string[4]. ( Strings are stored on length+1 bytes, byte at index 0 holding the current length. ( Pascal strings are not null-terminated. ) )

The first record in the file contains kind of header data, like record size in the file. It is 230 byte. The record you described is only 91 byte. ( Or 94 with my above mentioned type change. )

However I am still missing something. I will continue tomorrow.
 

Feherke.
http://free.rootshell.be/~feherke/

RE: Export .dat to txt. Help

Hi

Grr ! I knew it had to be something simple. Well, alignment must be turned off.

I added a second TextFile variable and formatted abit the output. So the .TXT file will look somehow readable and the .CSV file should be useful for importing in other applications.

After more analysis I decided to declare the Nregistro field as LongWord, so 4 byte long unsigned integer.

In the final big nothing at the end of the records one field can be identified of type Char. I put that in the something field.

The CtaContabil and LanCcusto fields are still looking bad as they often contain characters with code 0. Maybe CtaContabil is an array of Char...

So probably the record declaration is not the correct one. If you find a better one, or have idea about what should be there, let me know.

CODE

{$A-}

uses SysUtils;

type
  tabel=record
    Nregistro: LongWord;
    Codigo: string[15];
    Ccusto_red: string[6];
    Descricao: string[30];
    TipoClase: Char;
    NatClase: Char;
    CtaContabil: string[15];
    LanCcusto: Char;
    CtaContabil2: string[15];
    blahblah1: string[112];
    something: Char;
    blahblah2: string[22];
  end;

var
  filegegevens: file of tabel;
  recgegevens: tabel;
  output_text: TextFile;
  output_csv: TextFile;
  nr: Integer;

function quote(what:string):string;
begin
  quote:='"'+StringReplace(what,'"','""',[rfReplaceAll])+'"';
end;

begin
  Assign(filegegevens, 'CLASSES.DTA');
  Reset(filegegevens);
  Assign(output_text, 'CLASSES.TXT');
  if FileExists('CLASSES.TXT')
    then Append(output_text)
    else Rewrite(output_text);
  Assign(output_csv, 'CLASSES.CSV');
  if FileExists('CLASSES.CSV')
    then Append(output_csv)
    else Rewrite(output_csv);

  Read(filegegevens, recgegevens);
  nr:=0;
  while not Eof(filegegevens) do begin
    Read(filegegevens, recgegevens);
    Inc(nr);
    with recgegevens do begin
      Writeln(output_text, '---=[ ',nr,' ]=---');
      Writeln(output_text, 'Nregistro    ', Nregistro);
      Writeln(output_text, 'Codigo       ', Codigo);
      Writeln(output_text, 'CCusto_red   ', CCusto_red);
      Writeln(output_text, 'Descricao    ', Descricao);
      Writeln(output_text, 'TipoClase    ', TipoClase);
      Writeln(output_text, 'NatClase     ', NatClase);
      Writeln(output_text, 'CtaContabil  ', CtaContabil);
      Writeln(output_text, 'LanCcusto    ', LanCcusto);
      Writeln(output_text, 'CtaContabil2 ', CtaContabil2);
      Writeln(output_text, 'blahblah1    ', blahblah1);
      Writeln(output_text, 'something    ', something);
      Writeln(output_text, 'blahblah2    ', blahblah2);
    end;
    with recgegevens do begin
      Write(output_csv, Nregistro, ',');
      Write(output_csv, quote(Codigo), ',');
      Write(output_csv, quote(CCusto_red), ',');
      Write(output_csv, quote(Descricao), ',');
      Write(output_csv, quote(TipoClase), ',');
      Write(output_csv, quote(NatClase), ',');
      Write(output_csv, quote(CtaContabil), ',');
      Write(output_csv, quote(LanCcusto), ',');
      Write(output_csv, quote(CtaContabil2), ',');
      Writeln(output_csv, quote(something));
    end;
  end;

  Close(filegegevens);
  Close(output_text);
  Close(output_csv);
end.

Feherke.
http://free.rootshell.be/~feherke/

RE: Export .dat to txt. Help

(OP)
Feherke,

The code is working perfect, but as you can notice the output data is not the one that should be as attached.

I will double-triple check every thing again on the original program..

and work
 " So probably the record declaration is not the correct one "

how can the program help us.

Thank you very much.

PD: var (SHREK) and (DONKEY) i think got them. But var(FIOR) not. 7 try and not yet.

Sergio
 

RE: Export .dat to txt. Help

Hi

Quote (Sergio):

The code is working perfect, but as you can notice the output data is not the one that should be as attached.
You mean, saidasclasses2.TXT is the exact desired output ? I understood it is an example of incorrect output.

Quote (Sergio):

PD: var (SHREK) and (DONKEY) i think got them. But var(FIOR) not. 7 try and not yet.
It is another dot ( . ). But makes no difference as the mail probably not works. ( After the owner reinstalled the server, I not invested time in checking/configuring the mail. sad )
 

Feherke.
http://free.rootshell.be/~feherke/

RE: Export .dat to txt. Help

(OP)
Hello Feherke,

YES to=
"You mean, saidasclasses2.TXT is the exact desired output ?"

Exactly. I had attached the original .dat file  and the txt outputted from the program by it.

And our code is outputting wrong data.(now it is all about length i guess).

I ave checked again about the records declaration  of the tables  in the program in  two different places and the declaration is that i sent.

Thank you in advance.

Sergio

 

RE: Export .dat to txt. Help

Hi

If you comment out the Nregistro, LanCcusto and CtaContabil2 lines the output will have the same structure.

But not the same content. The CLASSES.DTA contains more records, some of them almost duplicated. So I suppose there are records marked as deleted, but physically still present. My best guess it that the Nregistro is 0 for active records and non-zero for deleted records. Skipping those, the content will be identical.

But not the same order. The records in your saidasclasses2.TXT are sorted in some way. Does that matter ?

CODE --> ( fragment )

    with recgegevens do begin
      if Nregistro<>0 then Continue;
//      Write(output_csv, Nregistro, ',');
      Write(output_csv, quote(Codigo), ',');
      Write(output_csv, quote(CCusto_red), ',');
      Write(output_csv, quote(Descricao), ',');
      Write(output_csv, quote(TipoClase), ',');
      Write(output_csv, quote(NatClase), ',');
      Write(output_csv, quote(CtaContabil), ',');
//      Write(output_csv, quote(LanCcusto), ',');
//      Write(output_csv, quote(CtaContabil2), ',');
      Writeln(output_csv, quote(something));
    end;
 

Feherke.
http://free.rootshell.be/~feherke/

RE: Export .dat to txt. Help

(OP)
Hi  Feherbe

Let´s focus on CSV.
I am a bit lost now. You say the output is similar, but with the code appended, I don´t achieve it. The order of the record is not important if the record has all fields OK. I can treat that later, but i am not being able to output what you are saying.

CODE




type
  tabel=record
    Nregistro: LongWord;
    Codigo: string[15];
    Ccusto_red: string[6];
    Descricao: string[30];
    TipoClase: Char;
    NatClase: Char;
    CtaContabil: string[15];
    LanCcusto: Char;
    CtaContabil2: string[15];
   // blahblah1: string[112];
      something: Char;
   // blahblah2: string[22];
  end;

var
  filegegevens: file of tabel;
  recgegevens: tabel;
 // output_text: TextFile;
  output_csv: TextFile;
  nr: Integer;

function quote(what:string):string;
begin
  quote:='"'+StringReplace(what,'"','""',[rfReplaceAll])+'"';
end;

begin
  Assign(filegegevens, 'C:\FPC\*\CLASSES.DTA');
  Reset(filegegevens);
 //Assign(output_text, 'C:\FPC\*\CLASSES.TXT');
 //f FileExists('CLASSES.TXT')
   //hen Append(output_text)
   // else Rewrite(output_text);
  Assign(output_csv, 'C:\FPC\*\CLASSES.CSV');
  if FileExists('CLASSES.CSV')
    then Append(output_csv)
    else Rewrite(output_csv);

  Read(filegegevens, recgegevens);
  nr:=0;
  while not Eof(filegegevens) do begin
    Read(filegegevens, recgegevens);
    with recgegevens do begin
      if Nregistro<>0 then Continue;
//      Write(output_csv, Nregistro, ',');
      Write(output_csv, quote(Codigo), ',');
      Write(output_csv, quote(CCusto_red), ',');
      Write(output_csv, quote(Descricao), ',');
      Write(output_csv, quote(TipoClase), ',');
      Write(output_csv, quote(NatClase), ',');
      Write(output_csv, quote(CtaContabil), ',');
//      Write(output_csv, quote(LanCcusto), ',');
//      Write(output_csv, quote(CtaContabil2), ',');
      Writeln(output_csv, quote(something));
    end;
  end;

  Close(filegegevens);
//  Close(output_text  );
  Close(output_csv);
end.

There are some lines to be comment but not able to know which in order to achieve your similar output to the saidasclasses2.TXT.
Is the quotes to be mantained??
S

Thanks in advance.

Sergio

RE: Export .dat to txt. Help

Hi

Just uncomment the declaration of blahblah fields. The actual record size in the CLASSES.DTA file is bigger than the structure you specified.

And if you actually removed the compilation directive and the uses clause, then them back.

Applying the above mentioned 3 modifications to the code the output matches your sample saidasclasses2.TXT :

CODE --> command line

master # fpc sergio.pas
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?

master # ./sergio

master # sort CLASSES.CSV | md5sum
8275d3fa2c0da2ef0cd7fc6d7dc0ba9d  -

master # sort saidasclasses2.TXT | dos2unix | md5sum
8275d3fa2c0da2ef0cd7fc6d7dc0ba9d  -
To avoid further misunderstandings here is the source code of the program :

CODE --> sergio.pas

{$A-}

uses SysUtils;

type
  tabel=record
    Nregistro: LongWord;
    Codigo: string[15];
    Ccusto_red: string[6];
    Descricao: string[30];
    TipoClase: Char;
    NatClase: Char;
    CtaContabil: string[15];
    LanCcusto: Char;
    CtaContabil2: string[15];
    blahblah1: string[112];
    something: Char;
    blahblah2: string[22];
  end;

var
  filegegevens: file of tabel;
  recgegevens: tabel;
  output_csv: TextFile;

function quote(what:string):string;
begin
  quote:='"'+StringReplace(what,'"','""',[rfReplaceAll])+'"';
end;

begin
  Assign(filegegevens, 'CLASSES.DTA');
  Reset(filegegevens);
  Assign(output_csv, 'CLASSES.CSV');
  if FileExists('CLASSES.CSV')
    then Append(output_csv)
    else Rewrite(output_csv);

  Read(filegegevens, recgegevens);
  while not Eof(filegegevens) do begin
    Read(filegegevens, recgegevens);
    with recgegevens do begin
      if Nregistro<>0 then Continue;
//      Write(output_csv, Nregistro, ',');
      Write(output_csv, quote(Codigo), ',');
      Write(output_csv, quote(CCusto_red), ',');
      Write(output_csv, quote(Descricao), ',');
      Write(output_csv, quote(TipoClase), ',');
      Write(output_csv, quote(NatClase), ',');
      Write(output_csv, quote(CtaContabil), ',');
//      Write(output_csv, quote(LanCcusto), ',');
//      Write(output_csv, quote(CtaContabil2), ',');
      Writeln(output_csv, quote(something));
    end;
  end;

  Close(filegegevens);
  Close(output_csv);
end.
 

Feherke.
http://free.rootshell.be/~feherke/

RE: Export .dat to txt. Help

(OP)
HiFeherke,

Thank you very much.

I´ll work on it and let you know.

Sergio

RE: Export .dat to txt. Help

(OP)
PERFECT PERFECT PERFECT PERFECT

You are GREAT Feherke, You did it!!!! Thank you man.

Well is working and outputting ok.!!!! Finally

But this is the easy part, the table we worked with is small (~7 records) and exports to txt, so it can be checked.

I will begin to work with bigger tables (~100 records) that do not export.So the challenge is huge.

Great Feherke taught me how to deal with the stuff, showed me how to catch the fish... and that is what i´m going to do.

In case i have other doubts i´ll contact you.

Sincerely thank you very much, i was stuck and lost and you came along and help me superbly.

Have a nice day.

Sergio



 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close