Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export .dat to txt. Help

Status
Not open for further replies.

sergio2011

Technical User
Jan 20, 2011
11
BR
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 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
 
Hi

Code:
[b]type[/b] tabel[teal]=[/teal][b]record[/b]
    Nregistro[teal]:[/teal]Word[teal];[/teal]
    Classe[teal]:[/teal]string[teal][[/teal][purple]15[/purple][teal]];[/teal]
    Ccusto_red[teal]:[/teal]string[teal][[/teal][purple]10[/purple][teal]];[/teal]
    Valor[teal]:[/teal][maroon]Real[/maroon][teal];[/teal]
    Ccusto_ext[teal]:[/teal]string[teal][[/teal][purple]15[/purple][teal]];[/teal]
    Def1[teal]:[/teal][maroon]Char[/maroon][teal];[/teal]
  [b]end[/b][teal];[/teal]

[b]var[/b]
  filegegevens[teal]:[/teal] [b]file[/b] [b]of[/b] tabel[teal];[/teal]
  recgegevens[teal]:[/teal] tabel[teal];[/teal]
  output_text[teal]:[/teal] [b]Text[/b][teal];[/teal]
  i[teal]:[/teal] [maroon]Integer[/maroon][teal];[/teal]

[b]begin[/b]

  [COLOR=darkgoldenrod]Assign[/color][teal]([/teal]filegegevens[teal],[/teal] [green][i]'c:\temp\data-log.dta'[/i][/green][teal]);[/teal]
  [b]Reset[/b][teal]([/teal]filegegevens[teal])[/teal]
  [COLOR=darkgoldenrod]Assign[/color][teal]([/teal]output_text[teal],[/teal] [green][i]'c:\temp\data-log.txt'[/i][/green][teal]);[/teal]
  [b]Rewrite[/b][teal]([/teal]output_text[teal]);[/teal]

  [b]while[/b] [b]not[/b] [COLOR=darkgoldenrod]Eof[/color][teal]([/teal]filegegevens[teal])[/teal] [b]do[/b] [b]begin[/b]
    [b]Read[/b][teal]([/teal]filegegevens[teal],[/teal] recgegevens[teal]);[/teal]
    [b]with[/b] recgegevens [b]do[/b] [b]begin[/b]
      [b]Writeln[/b][teal]([/teal]output_text[teal],[/teal] Nregistro[teal]);[/teal]
      [b]Writeln[/b][teal]([/teal]output_text[teal],[/teal] Classe[teal]);[/teal]
      [b]Writeln[/b][teal]([/teal]output_text[teal],[/teal] CCusto_red[teal]);[/teal]
      [b]Writeln[/b][teal]([/teal]output_text[teal],[/teal] Valor[teal]);[/teal]
      [b]Writeln[/b][teal]([/teal]output_text[teal],[/teal] CCusto_ext[teal]);[/teal]
      [b]Writeln[/b][teal]([/teal]output_text[teal],[/teal] Def1[teal]);[/teal]
    [b]end[/b][teal];[/teal]
  [b]end[/b][teal];[/teal]

  [COLOR=darkgoldenrod]Close[/color][teal]([/teal]filegegevens[teal]);[/teal]
  [COLOR=darkgoldenrod]Close[/color][teal]([/teal]output_text[teal]);[/teal]

[b]end[/b][teal].[/teal]


Feherke.
 
Hi Feherke,

wooooooohh Looks great....

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

Thank you
 
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

 
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...
 
Hi

Sergio said:
(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. )
Sergio said:
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 [tt]Rewrite[/tt] procedure with [tt]Append[/tt]. But because [tt]Append[/tt] fails if the file not exists, you will still need [tt]Rewrite[/tt], but controlled by a condition :
Code:
[b]if[/b] [COLOR=darkgoldenrod]FileExists[/color][teal]([/teal][green][i]'c:\temp\data-log.txt'[/i][/green][teal])[/teal]
[b]then[/b] [COLOR=darkgoldenrod]Append[/color][teal]([/teal]output_text[teal])[/teal]
[b]else[/b] [COLOR=darkgoldenrod]Rewrite[/color][teal]([/teal]output_text[teal]);[/teal]
To have the [tt]FileExists[/tt] function available add this line to the beginning of the source code :
Code:
[b]uses[/b] sysutils[teal];[/teal]
Sergio said:
Gives error 3 on exiting the program...
I/O error 3 means "Path not found". Check the path used in then [tt]Assign[/tt] procedures.


Feherke.
 
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
 
 http//FILE:C:\FPC\Onmium\Omnium\MAXIMO\CLASSES.dta
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:
output_text[teal]:[/teal] TextFile[teal];[/teal]

Feherke.
 
Hi

Sergio said:
Nregistro word 5
That is not a [tt]Word[/tt]. [tt]Word[/tt] 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 [tt]string[4][/tt]. ( 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.
 
Hi

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

I added a second [tt]TextFile[/tt] 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 [tt]LongWord[/tt], so 4 byte long unsigned integer.

In the final big nothing at the end of the records one field can be identified of type [tt]Char[/tt]. 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 [tt]array[/tt] of [tt]Char[/tt]...

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:
[gray]{$A-}[/gray]

[b]uses[/b] SysUtils[teal];[/teal]

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

[b]var[/b]
  filegegevens[teal]:[/teal] [b]file[/b] [b]of[/b] tabel[teal];[/teal]
  recgegevens[teal]:[/teal] tabel[teal];[/teal]
  output_text[teal]:[/teal] TextFile[teal];[/teal]
  output_csv[teal]:[/teal] TextFile[teal];[/teal]
  nr[teal]:[/teal] [maroon]Integer[/maroon][teal];[/teal]

[b]function[/b] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]what[teal]:[/teal][b]string[/b][teal]):[/teal][b]string[/b][teal];[/teal]
[b]begin[/b]
  quote[teal]:=[/teal][green][i]'"'[/i][/green][teal]+[/teal][COLOR=darkgoldenrod]StringReplace[/color][teal]([/teal]what[teal],[/teal][green][i]'"'[/i][/green][teal],[/teal][green][i]'""'[/i][/green][teal],[[/teal]rfReplaceAll[teal]])+[/teal][green][i]'"'[/i][/green][teal];[/teal]
[b]end[/b][teal];[/teal]

[b]begin[/b]
  [COLOR=darkgoldenrod]Assign[/color][teal]([/teal]filegegevens[teal],[/teal] [green][i]'CLASSES.DTA'[/i][/green][teal]);[/teal]
  [COLOR=darkgoldenrod]Reset[/color][teal]([/teal]filegegevens[teal]);[/teal]
  [COLOR=darkgoldenrod]Assign[/color][teal]([/teal]output_text[teal],[/teal] [green][i]'CLASSES.TXT'[/i][/green][teal]);[/teal]
  [b]if[/b] [COLOR=darkgoldenrod]FileExists[/color][teal]([/teal][green][i]'CLASSES.TXT'[/i][/green][teal])[/teal]
    [b]then[/b] [COLOR=darkgoldenrod]Append[/color][teal]([/teal]output_text[teal])[/teal]
    [b]else[/b] [COLOR=darkgoldenrod]Rewrite[/color][teal]([/teal]output_text[teal]);[/teal]
  [COLOR=darkgoldenrod]Assign[/color][teal]([/teal]output_csv[teal],[/teal] [green][i]'CLASSES.CSV'[/i][/green][teal]);[/teal]
  [b]if[/b] [COLOR=darkgoldenrod]FileExists[/color][teal]([/teal][green][i]'CLASSES.CSV'[/i][/green][teal])[/teal]
    [b]then[/b] [COLOR=darkgoldenrod]Append[/color][teal]([/teal]output_csv[teal])[/teal]
    [b]else[/b] [COLOR=darkgoldenrod]Rewrite[/color][teal]([/teal]output_csv[teal]);[/teal]

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

  [COLOR=darkgoldenrod]Close[/color][teal]([/teal]filegegevens[teal]);[/teal]
  [COLOR=darkgoldenrod]Close[/color][teal]([/teal]output_text[teal]);[/teal]
  [COLOR=darkgoldenrod]Close[/color][teal]([/teal]output_csv[teal]);[/teal]
[b]end[/b][teal].[/teal]

Feherke.
 
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
 
Hi

Sergio said:
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.
Sergio said:
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. :-( )


Feherke.
 
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

 
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:
    [b]with[/b] recgegevens [b]do[/b] [b]begin[/b]
      [b]if[/b] Nregistro[teal]<>[/teal][purple]0[/purple] [b]then[/b] Continue[teal];[/teal]
[gray]//      Write(output_csv, Nregistro, ',');[/gray]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]Codigo[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]CCusto_red[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]Descricao[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]TipoClase[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]NatClase[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]CtaContabil[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
[gray]//      Write(output_csv, quote(LanCcusto), ',');[/gray]
[gray]//      Write(output_csv, quote(CtaContabil2), ',');[/gray]
      [COLOR=darkgoldenrod]Writeln[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]something[teal]));[/teal]
    [b]end[/b][teal];[/teal]


Feherke.
 
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
 
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 [tt]uses[/tt] clause, then them back.

Applying the above mentioned 3 modifications to the code the output matches your sample saidasclasses2.TXT :
Code:
[blue]master #[/blue] fpc sergio.pas 
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?

[blue]master #[/blue] ./sergio 

[blue]master #[/blue] sort CLASSES.CSV | md5sum 
8275d3fa2c0da2ef0cd7fc6d7dc0ba9d  -

[blue]master #[/blue] sort saidasclasses2.TXT | dos2unix | md5sum 
8275d3fa2c0da2ef0cd7fc6d7dc0ba9d  -
To avoid further misunderstandings here is the source code of the program :
Code:
[gray]{$A-}[/gray]

[b]uses[/b] SysUtils[teal];[/teal]

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

[b]var[/b]
  filegegevens[teal]:[/teal] [b]file[/b] [b]of[/b] tabel[teal];[/teal]
  recgegevens[teal]:[/teal] tabel[teal];[/teal]
  output_csv[teal]:[/teal] TextFile[teal];[/teal]

[b]function[/b] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]what[teal]:[/teal][b]string[/b][teal]):[/teal][b]string[/b][teal];[/teal]
[b]begin[/b]
  quote[teal]:=[/teal][green][i]'"'[/i][/green][teal]+[/teal][COLOR=darkgoldenrod]StringReplace[/color][teal]([/teal]what[teal],[/teal][green][i]'"'[/i][/green][teal],[/teal][green][i]'""'[/i][/green][teal],[[/teal]rfReplaceAll[teal]])+[/teal][green][i]'"'[/i][/green][teal];[/teal]
[b]end[/b][teal];[/teal]

[b]begin[/b]
  [COLOR=darkgoldenrod]Assign[/color][teal]([/teal]filegegevens[teal],[/teal] [green][i]'CLASSES.DTA'[/i][/green][teal]);[/teal]
  [COLOR=darkgoldenrod]Reset[/color][teal]([/teal]filegegevens[teal]);[/teal]
  [COLOR=darkgoldenrod]Assign[/color][teal]([/teal]output_csv[teal],[/teal] [green][i]'CLASSES.CSV'[/i][/green][teal]);[/teal]
  [b]if[/b] [COLOR=darkgoldenrod]FileExists[/color][teal]([/teal][green][i]'CLASSES.CSV'[/i][/green][teal])[/teal]
    [b]then[/b] [COLOR=darkgoldenrod]Append[/color][teal]([/teal]output_csv[teal])[/teal]
    [b]else[/b] [COLOR=darkgoldenrod]Rewrite[/color][teal]([/teal]output_csv[teal]);[/teal]

  [COLOR=darkgoldenrod]Read[/color][teal]([/teal]filegegevens[teal],[/teal] recgegevens[teal]);[/teal]
  [b]while[/b] [b]not[/b] [COLOR=darkgoldenrod]Eof[/color][teal]([/teal]filegegevens[teal])[/teal] [b]do[/b] [b]begin[/b]
    [COLOR=darkgoldenrod]Read[/color][teal]([/teal]filegegevens[teal],[/teal] recgegevens[teal]);[/teal]
    [b]with[/b] recgegevens [b]do[/b] [b]begin[/b]
      [b]if[/b] Nregistro[teal]<>[/teal][purple]0[/purple] [b]then[/b] Continue[teal];[/teal]
[gray]//      Write(output_csv, Nregistro, ',');[/gray]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]Codigo[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]CCusto_red[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]Descricao[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]TipoClase[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]NatClase[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
      [COLOR=darkgoldenrod]Write[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]CtaContabil[teal]),[/teal] [green][i]','[/i][/green][teal]);[/teal]
[gray]//      Write(output_csv, quote(LanCcusto), ',');[/gray]
[gray]//      Write(output_csv, quote(CtaContabil2), ',');[/gray]
      [COLOR=darkgoldenrod]Writeln[/color][teal]([/teal]output_csv[teal],[/teal] [COLOR=darkgoldenrod]quote[/color][teal]([/teal]something[teal]));[/teal]
    [b]end[/b][teal];[/teal]
  [b]end[/b][teal];[/teal]

  [COLOR=darkgoldenrod]Close[/color][teal]([/teal]filegegevens[teal]);[/teal]
  [COLOR=darkgoldenrod]Close[/color][teal]([/teal]output_csv[teal]);[/teal]
[b]end[/b][teal].[/teal]


Feherke.
 
HiFeherke,

Thank you very much.

I´ll work on it and let you know.

Sergio
 
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



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top