Recent

Author Topic: need help with creating a file like this  (Read 15030 times)

GMP_47

  • Jr. Member
  • **
  • Posts: 60
need help with creating a file like this
« on: October 11, 2015, 09:45:04 pm »
In my previous post (http://forum.lazarus.freepascal.org/index.php/topic,29740.0.html) I asked for help for showing a .pas with that type.
Now I'm doing one that creates a .dat similar to that, but I cant get the SystemDate correctly:


Code: Pascal  [Select][+][-]
  1. program Crear;
  2. uses SysUtils;
  3. type
  4.   THeader = record
  5.     Serial: Word;   //2 bytes
  6.     Filename: String[255];  //1 byte
  7.     Date: Word;  //2 bytes
  8.     Fieldnrs: Word;  //2 bytes
  9.     NrsRecords: Word;  //2 bytes
  10.   end;
  11.   TFields = record
  12.     Fieldnr: Word;   //2 bytes
  13.     Fieldname: ShortString;   //1 byte
  14.   end;
  15.   TRecordField = record
  16.     Fieldnr: Word;  //2 bytes
  17.     FieldText: String[255];  //1 byte
  18.   end;
  19.   TDateTime = double;
  20.  
  21. var
  22.   BinaryStream: File;
  23.   Header: THeader;                     //Header.Serial Filename Date Fieldnrs NrsRecords
  24.   Fields: array [0..20] of TFields;    //Fields.Fieldnr     Fields.Fieldname
  25.   Rec: array[0..20] of TRecordField;   //Rec.Fieldnr        Rec.FieldText
  26.   Len: Byte;
  27.   nrs: Integer;  //2 bytes
  28.   I,N: Integer;
  29.   cant_reg: word;
  30.  
  31. procedure myWrite (var Buffer; Size: Integer);
  32. begin
  33.      BlockWrite(BinaryStream, Buffer, Size);
  34. end;
  35.  
  36. procedure myWriteWord (var W:Word);
  37. begin
  38.      myWrite(W,2);
  39.      W := Swap(W);
  40. end;
  41.  
  42. procedure myWriteString (var S:ShortString);
  43. begin
  44.      myWrite (Len,1);
  45.      S[0] := Chr(Len);
  46.      myWrite (S[1], Len);
  47. end;
  48.  
  49.  
  50.  
  51. function encode_date ( ddate:string ): Word;
  52.  
  53. var
  54.    Year,anio : integer;
  55.    Month,Day,dia, mes, y_offset: byte;
  56. begin
  57.      DeCodeDate (Date,Year,Month,Day);
  58.      if year>2000 then
  59.           y_offset :=  year - 2000
  60.         else
  61.           y_offset := year - 1999 + 100;
  62.      end;
  63.  
  64.       y_offset := (ddate  AND %0000000111111111) shl 7;
  65.       mes      := (ddate AND  %0000000011110000) shl 5;
  66.       dia      := (ddate  AND %0000000000011111);
  67.       result   := (ddate);
  68. end;
  69.  
  70.  
  71. begin
  72.      assign(BinaryStream, 'C:\Dev-Pas\EXAMEN333.dat');
  73.      rewrite(BinaryStream, 1);
  74.      Writeln ('Ingrese Serial:');
  75.      ReadLn (Header.Serial);
  76.      myWriteWord (Header.Serial);
  77.  
  78.      Writeln ('Ingrese Nombre Archivo:');
  79.      ReadLn (Header.Filename);
  80.      myWriteString (Header.Filename);
  81.  
  82.      ReadLn (encode_date(Header.Date));
  83.      myWriteWord (Header.Date);
  84.  
  85.      Writeln ('Ingrese Cantidad de Campos Customizados:');
  86.      ReadLn (Header.Fieldnrs);
  87.      myWriteWord (Header.Fieldnrs);
  88.  
  89.      for nrs := 0 to Header.Fieldnrs - 1 do
  90.      begin
  91.           Writeln ('Ingrese Nombre de dato numero:', nrs+1);
  92.           myWriteWord (Fields[nrs].Fieldnr);
  93.           ReadLn (Fields[nrs].Fieldname);
  94.      end;
  95.  
  96.      cant_reg=0;
  97.  
  98.      Writeln ('Ingrese datos de contactos o escriba FIN para finalizar');
  99.  
  100.      for I := 0 to Header.Fieldnrs - 1 do
  101.      begin
  102.  
  103.           Writeln ('Ingrese:', Fields[I].Fieldname);
  104.           ReadLn (Rec[I].FieldText);
  105.  
  106.           While Rec[I].FieldText not FIN do begin
  107.                  if Length (Rec[I].FieldText) <> 0 then
  108.                         if Length (Rec[I].FieldText) < 255 then
  109.                                myWriteString (Rec[I].FieldText)
  110.                            else
  111.                                Writeln ('Debe ser Menos de 255 caracteres');
  112.                         end;
  113.                     end;
  114.  
  115.                 cant_reg+1;
  116.                 Writeln ('Cant. de registros ingresados:',cant_reg );
  117.           end;
  118.  
  119.   myWriteWord (Header.NrsRecords := cant_reg );
  120.   close (BinaryStream);
  121. end.
  122.  
  123.  
  124.  
  125.  

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: need help with creating a file like this
« Reply #1 on: October 11, 2015, 10:35:14 pm »
First of all I have a remark about this:
Code: Pascal  [Select][+][-]
  1. procedure myWriteWord (var W:Word);
  2. begin
  3.   myWrite(W,2);
  4.   W := Swap(W);
  5. end;
Here you first write out the Word and THEN you do a swap(). Do you think that's the correct order to do that?

Second is this:
Code: Pascal  [Select][+][-]
  1. procedure myWriteString (var S:ShortString);
  2. begin
  3.   myWrite (Len,1);
  4.   S[0] := Chr(Len);
  5.   myWrite (S[1], Len);
  6. end;
This won't work either. Len has not yet been initialized with a value when you write it. And you don't need to set the S[0] because the string already has a length. So all you need to do is set Len to the length of the string (as first statement) and remove the S[0]-line.

Then your encode_date function. That won't even compile. Look at the begin and the ends.
Code: Pascal  [Select][+][-]
  1.      if year>2000 then
  2.           y_offset :=  year - 2000
  3.         else
  4.           y_offset := year - 1999 + 100;
  5.      end;
Here you have an end without a begin.

Then the function itself:
Code: Pascal  [Select][+][-]
  1. function encode_date ( ddate:string ): Word;
  2. var
  3.    Year,anio : integer;
  4.    Month,Day,dia, mes, y_offset: byte;
  5. begin
  6.      DeCodeDate (Date,Year,Month,Day);
  7.  
You do a DecodeDate(Date... as first function call in this function. But what is Date? You have a ddate you give to this function. If that's the date you want to use you need to make that a TDateTime and call DecodeDate with ddate. Although... if you want to always set the date to Today then you can use Date but in that case you don't need to ask the user for the date. You can remove the readln for date and remove the parameter ddate from your function in that case. B.T.W. the types of Year,Month,Day are wrong for this function. They all need to be Word (and not byte and integer).

Next your order of first decode and determining y_offset etc is correct. But this line seems off:
Code: Pascal  [Select][+][-]
  1. y_offset := year - 1999 + 100;
In your pdf it said it needed 125 to be 1974.
In this case 1974 - 1999 + 100 = 75. (So you did something wrong here)
Redo that line.

And finally.... the bits :)
Code: Pascal  [Select][+][-]
  1.       y_offset := (ddate  AND %0000000111111111) shl 7;
  2.       mes      := (ddate AND  %0000000011110000) shl 5;
  3.       dia      := (ddate  AND %0000000000011111);
  4.       result   := (ddate);
y_offset already has a value so why are you trying to calculate a new value from ddate? And the other 2 lines also don't make sense. What are you going to do with dia and mes?

I'll help you on your way. You only need to do something with y_offset, month and day to calculate and set into result, like this:
Code: Pascal  [Select][+][-]
  1. result := y_offset......
  2. result := result + month......
  3. result := result + day......
And as a big hint. You only need to use shl with these lines. See where y_offset needs to be in your Word and shift accordingly.

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with creating a file like this
« Reply #2 on: October 12, 2015, 12:19:44 am »
getting error with result:
Code: Pascal  [Select][+][-]
  1. program Crear;
  2. uses SysUtils;
  3. type
  4.   THeader = record
  5.     Serial: Word;   //2 bytes
  6.     Filename: String[255];  //1 byte
  7.     Date: Word;  //2 bytes
  8.     Fieldnrs: Word;  //2 bytes
  9.     NrsRecords: Word;  //2 bytes
  10.   end;
  11.   TFields = record
  12.     Fieldnr: Word;   //2 bytes
  13.     Fieldname: ShortString;   //1 byte
  14.   end;
  15.   TRecordField = record
  16.     Fieldnr: Word;  //2 bytes
  17.     FieldText: String[255];  //1 byte
  18.   end;
  19.   TDateTime = double;
  20.  
  21. var
  22.   BinaryStream: File;
  23.   Header: THeader;                     //Header.Serial Filename Date Fieldnrs NrsRecords
  24.   Fields: array [0..20] of TFields;    //Fields.Fieldnr     Fields.Fieldname
  25.   Rec: array[0..20] of TRecordField;   //Rec.Fieldnr        Rec.FieldText
  26.   Len: Byte;
  27.   nrs: Integer;  //2 bytes
  28.   I,N: Integer;
  29.   cant_reg: word;
  30.  
  31. procedure myWrite (var Buffer; Size: Integer);
  32. begin
  33.      BlockWrite(BinaryStream, Buffer, Size);
  34. end;
  35.  
  36. procedure myWriteWord (var W:Word);
  37. begin
  38.      W := Swap(W);
  39.      myWrite(W,2);
  40. end;
  41.  
  42. procedure myWriteString (var S:ShortString);
  43. begin
  44.      Len := Length(S);
  45.      myWrite (Len,1);
  46.      myWrite (S[1], Len);
  47. end;
  48.  
  49.  
  50.  
  51. procedure encode_date ( var Year,Month,Day,y_offset: Word);
  52. begin
  53.      DeCodeDate (Date,Year,Month,Day);
  54.      if year > 2000 then begin
  55.           y_offset :=  year - 2000;
  56.         end else begin
  57.           y_offset := 2099 - year;
  58.      end;
  59.      result := y_offset (0000000001111111) shl 9;
  60.      result := result + month (0000000000001111) shl 5;
  61.      result := result + day
  62. end;
  63.  
  64.  
  65. begin
  66.      assign(BinaryStream, 'C:\Dev-Pas\EXAMEN333.dat');
  67.      rewrite(BinaryStream, 1);
  68.      Writeln ('Ingrese Serial:');
  69.      ReadLn (Header.Serial);
  70.      myWriteWord (Header.Serial);
  71.  
  72.      Writeln ('Ingrese Nombre Archivo:');
  73.      ReadLn (Header.Filename);
  74.      myWriteString (Header.Filename);
  75.  
  76.      myWriteWord(encode_date(Header.Date));
  77.  
  78.      Writeln ('Ingrese Cantidad de Campos Customizados:');
  79.      ReadLn (Header.Fieldnrs);
  80.      myWriteWord (Header.Fieldnrs);
  81.  
  82.      for nrs := 0 to Header.Fieldnrs - 1 do
  83.      begin
  84.           Writeln ('Ingrese Nombre de dato numero:', nrs+1);
  85.           myWriteWord (Fields[nrs].Fieldnr);
  86.           ReadLn (Fields[nrs].Fieldname);
  87.      end;
  88.  
  89.      cant_reg=0;
  90.  
  91.      Writeln ('Ingrese datos de contactos o escriba FIN para finalizar');
  92.  
  93.      for I := 0 to Header.Fieldnrs - 1 do
  94.      begin
  95.  
  96.           Writeln ('Ingrese:', Fields[I].Fieldname);
  97.           ReadLn (Rec[I].FieldText);
  98.  
  99.           While Rec[I].FieldText not FIN do begin
  100.                  if Length (Rec[I].FieldText) <> 0 then
  101.                         if Length (Rec[I].FieldText) < 255 then
  102.                                myWriteString (Rec[I].FieldText)
  103.                            else
  104.                                Writeln ('Debe ser Menos de 255 caracteres');
  105.                         end;
  106.                     end;
  107.  
  108.                 cant_reg+1;
  109.                 Writeln ('Cant. de registros ingresados:',cant_reg );
  110.           end;
  111.  
  112.   myWriteWord (Header.NrsRecords := cant_reg );
  113.   close (BinaryStream);
  114. end.
  115.  
  116.  
  117.  
  118.  

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: need help with creating a file like this
« Reply #3 on: October 12, 2015, 12:28:26 am »
rvk, the problem here is he doesn't think as programmer at all (he doesn't follow any logic) or my be he doesn't try at all
Code: [Select]
1999-a+100 = 1974
1999-a+100 = year
what is `a`? very simple
I feel like he throwing lines of codes at random copying and pasting then asking people to correct him (might as well give him the full code). he doesn't try to understand the code at all.

GMP_47, you need to read more about basics of programming (not pascal specifically) and think in logical serialized steps to produce solution to problem.
this may help you http://www.ceebot.com/ceebot/family-e.php
« Last Edit: October 12, 2015, 12:30:21 am by shobits1 »

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with creating a file like this
« Reply #4 on: October 12, 2015, 12:35:52 am »
rvk, the problem here is he doesn't think as programmer at all (he doesn't follow any logic) or my be he doesn't try at all
Code: [Select]
1999-a+100 = 1974
1999-a+100 = year
what is `a`? very simple
I feel like he throwing lines of codes at random copying and pasting then asking people to correct him (might as well give him the full code). he doesn't try to understand the code at all.

GMP_47, you need to read more about basics of programming (not pascal specifically) and think in logical serialized steps to produce solution to problem.
this may help you http://www.ceebot.com/ceebot/family-e.php
I was told to ask for help. As strange that mind sound. Also mind that english isnt my native language.

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: need help with creating a file like this
« Reply #5 on: October 12, 2015, 12:38:22 am »
What error do you get and on what line?
And how do you think you need to fix it?

(Just saying you get an error and hoping other would fix it won't help you much)

B.T.W. You still have that extra end in that function and you don't need the 0000000111111s, only the shls. (You don't even have valid operators on that line for those 000001111s.)
« Last Edit: October 12, 2015, 12:40:15 am by rvk »

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: need help with creating a file like this
« Reply #6 on: October 12, 2015, 12:52:19 am »
Quote
I was told to ask for help. As strange that mind sound. Also mind that english isnt my native language.
asking for help is alright but you need to show that you actually trying to solve the problem yourself; also programming is not english (language ), and  I'm sure there plenty of Spanish (if it is your language) sites to teach about basics of programming;
sorry for being harsh with you; but as I said before, the problem here is not the assignment being hard (although it has it's challenges) but it is clear you do not handle the language (pascal) well, or you do not have clear idea of the assignment or you do not think as one should do when programming (this is what I fear the most).

FYI, english is not my native language either.

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with creating a file like this
« Reply #7 on: October 12, 2015, 01:12:37 am »
What error do you get and on what line?
And how do you think you need to fix it?

(Just saying you get an error and hoping other would fix it won't help you much)

B.T.W. You still have that extra end in that function and you don't need the 0000000111111s, only the shls. (You don't even have valid operators on that line for those 000001111s.)

Identifier not found RESULT

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: need help with creating a file like this
« Reply #8 on: October 12, 2015, 01:19:48 am »
Identifier not found RESULT
And what do you think the problem is?
Result is a automatic variable you can assign your value to as a result for the function. But.... are you in a function there? (hint: You changed the function to a procedure)

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with creating a file like this
« Reply #9 on: October 12, 2015, 01:49:15 am »
this is not giving an error. just want to know if it's fine
Code: Pascal  [Select][+][-]
  1. BlockRead(BinaryStream, Header.Date, 2);
  2.      myWriteWord (Header.Date);

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: need help with creating a file like this
« Reply #10 on: October 12, 2015, 01:53:35 am »
I'm not sure what you're trying to do there. It reads a word out of a binary file and directly afterwards writes a word to that binary file. What do you want it to here?

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with creating a file like this
« Reply #11 on: October 12, 2015, 02:24:29 am »
I need to make the loop close somehow.
I was thinking in making While Rec.FieldText NOT FIN do begin (the user stops adding info when FIN is typed).

Code: Pascal  [Select][+][-]
  1. program Crear;
  2. uses SysUtils;
  3. type
  4.   THeader = record
  5.     Serial: Word;   //2 bytes
  6.     Filename: String[255];  //1 byte
  7.     Date: Word;  //2 bytes
  8.     Fieldnrs: Word;  //2 bytes
  9.     NrsRecords: Word;  //2 bytes
  10.   end;
  11.   TFields = record
  12.     Fieldnr: Word;   //2 bytes
  13.     Fieldname: ShortString;   //1 byte
  14.   end;
  15.   TRecordField = record
  16.     Fieldnr: Word;  //2 bytes
  17.     FieldText: String[255];  //1 byte
  18.   end;
  19.   TDateTime = double;
  20.  
  21. var
  22.   BinaryStream: File;
  23.   Header: THeader;                     //Header.Serial Filename Date Fieldnrs NrsRecords
  24.   Fields: array [0..20] of TFields;    //Fields.Fieldnr     Fields.Fieldname
  25.   Rec: array[0..20] of TRecordField;   //Rec.Fieldnr        Rec.FieldText
  26.   Len: Byte;
  27.   nrs: Integer;  //2 bytes
  28.   I,N: Integer;
  29.   cant_reg: word;
  30.   fecha,Year,Month,Day,y_offset: Word;
  31.  
  32. procedure myWrite (var Buffer; Size: Integer);
  33. begin
  34.      BlockWrite(BinaryStream, Buffer, Size);
  35. end;
  36.  
  37. procedure myWriteWord (var W:Word);
  38. begin
  39.      W := Swap(W);
  40.      myWrite(W,2);
  41. end;
  42.  
  43.  
  44. procedure myWriteString (var S:ShortString);
  45. begin
  46.      Len := Length(S);
  47.      myWrite (Len,1);
  48.      myWrite (S[1], Len);
  49. end;
  50.  
  51. procedure encode_date ( var fecha,Year,Month,Day,y_offset: Word);
  52. begin
  53.      DeCodeDate (Date,Year,Month,Day);
  54.      if year > 2000 then begin
  55.           y_offset :=  year - 2000;
  56.         end else begin
  57.           y_offset := 2099 - year;
  58.      end;
  59.      fecha := y_offset  shl 9;
  60.      fecha := fecha + month  shl 5;
  61.      fecha := fecha + day
  62. end;
  63.  
  64.  
  65.  
  66. begin
  67.      assign(BinaryStream, 'C:\Dev-Pas\EXAMEN333.dat');
  68.      rewrite(BinaryStream, 1);
  69.      Writeln ('Ingrese Serial:');
  70.      ReadLn (Header.Serial);
  71.      myWriteWord (Header.Serial);
  72.  
  73.      Writeln ('Ingrese Nombre Archivo:');
  74.      ReadLn (Header.Filename);
  75.      myWriteString (Header.Filename);
  76.  
  77.  
  78.      encode_date ( fecha,Year,Month,Day,y_offset );
  79.      myWriteWord (Header.Date);                                            //fecha
  80.  
  81.      Writeln ('Ingrese Cantidad de Campos Customizados:');
  82.      ReadLn (Header.Fieldnrs);
  83.      myWriteWord (Header.Fieldnrs);
  84.  
  85.  
  86.      for nrs := 0 to Header.Fieldnrs - 1 do
  87.      begin
  88.           Writeln ('Ingrese Nombre de dato numero:', nrs+1);
  89.           myWriteWord (Fields[nrs].Fieldnr);
  90.           ReadLn (Fields[nrs].Fieldname);
  91.      end;
  92.  
  93.  
  94.      cant_reg:=0;
  95.      Writeln ('Ingrese datos de contactos o escriba FIN para finalizar');
  96.  
  97.      for I := 0 to Header.Fieldnrs - 1 do
  98.      begin
  99.  
  100.           Writeln ('Ingrese:', Fields[I].Fieldname);
  101.           ReadLn (Rec[I].FieldText);
  102.  
  103.           While Rec[I].FieldText NOT FIN do begin
  104.                  if Length (Rec[I].FieldText) <> 0 then
  105.                         if Length (Rec[I].FieldText) < 255 then
  106.                                myWriteString (Rec[I].FieldText);
  107.                            else
  108.                                Writeln ('Debe ser Menos de 255 caracteres');
  109.                         end;
  110.                     end;
  111.  
  112.                 inc(cant_reg);
  113.                 Writeln ('Cant. de registros ingresados:',cant_reg );
  114.           end;
  115.      end;
  116. myWriteWord (Header.NrsRecords := cant_reg );
  117. close (BinaryStream);
  118. end.
  119.  
  120.  
  121.  
  122.  

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: need help with creating a file like this
« Reply #12 on: October 12, 2015, 12:01:14 pm »
I need to make the loop close somehow.
I was thinking in making While Rec.FieldText NOT FIN do begin (the user stops adding info when FIN is typed).
Okay... first things first.
I see you will be using a procedure for encode_date and not a function. That is your choice but I hope you know the difference between a procedure and a function. If not, then that's one of the things you might want to read up on.

The second thing is this:
Code: Pascal  [Select][+][-]
  1.      encode_date ( fecha,Year,Month,Day,y_offset );
  2.      myWriteWord (Header.Date);                                            //fecha
You give Year, Month and y_offset as parameter to this procedure. Why? You don't use it anywhere in the rest of your program. That leads me to my second suggestion for "homework". You might want to lean what "local variables" are. You keep putting your variables (which we already declared in the function in our examples) at the top of your program. You need to learn that if you only use variables within a procedure or function you need to declare them there (or at least it's better coding). For instance that Len variable you use in myWriteString. You only use it in myWriteString so you can declare it in myWriteString itself (as I originally did with myReadString). Don't do it at the top of your program. The same goes for Year, Month and y_offset. In that case you don't even need to have these as parameter for the call.

Then those lines above again:
Code: Pascal  [Select][+][-]
  1.      encode_date ( fecha,Year,Month,Day,y_offset );
  2.      myWriteWord (Header.Date);                                            //fecha
You have fecha, which is calculated in the encode_date. What do you do with it??? I don't see you using it. You go on writing Header.Date, but that's not initialized yet. You need to put the value fecha into Header.Date before you write Header.Date to the file.

Next part:
Code: Pascal  [Select][+][-]
  1.      for nrs := 0 to Header.Fieldnrs - 1 do
  2.      begin
  3.           Writeln ('Ingrese Nombre de dato numero:', nrs+1);
  4.           myWriteWord (Fields[nrs].Fieldnr);
  5.           ReadLn (Fields[nrs].Fieldname);
  6.      end;
You read Header.Fieldnrs from user input. Ok. Next you loop the variable nrs to get the name of all the fields. But you writeout Fields[nrs].Fieldnr, which hasn't any value yet. So first put the nrs value in Fields[nrs].Fieldnr before writing it to disk. Next you read the Fields[nrs].Fieldname. Shouldn't you write that to disk too???

Then the next part:
Code: Pascal  [Select][+][-]
  1.      cant_reg:=0;
  2.      Writeln ('Ingrese datos de contactos o escriba FIN para finalizar');
After this you go on reading the contacts themselves. But according to the documentation you need to first write the number of contacts you're going to write. So first ask the user how many contacts (s)he's going to enter and write that number to disk.

Then your problem line:
Code: Pascal  [Select][+][-]
  1. While Rec[I].FieldText NOT FIN do begin
I'm not sure what you're trying to do here.
If it's something that a user can enter "FIN" then you should've done this
Code: Pascal  [Select][+][-]
  1. While Rec[I].FieldText <> 'FIN' do begin
But... that's not going to work in your case. Because the user FIRST needs to enter the number of contacts (s)he's going to enter, ending with "FIN" doesn't work.

You need to loop (for/next) for NumberOfContacts the user entered. The user can't break off the input before (s)he's has entered all contacts.

So above the for I := 0 to Header.Fieldnrs - 1 you need to do something like this:
Code: Pascal  [Select][+][-]
  1. for J := 1 to NumberOfContacts do
  2. begin
  3.   for I := 0 to Header.Fieldnrs - 1 do
  4.   begin
  5.     //....

I hope you take my suggestion of learning the difference in procedure and function and working with local variables to heart and you can do something useful with the notes I gave above.
« Last Edit: October 12, 2015, 02:44:20 pm by rvk »

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with creating a file like this
« Reply #13 on: October 12, 2015, 05:40:00 pm »
I reviewed the difference between procedures and functions just in case.
what I get messed up with is
Date gives TDateTime; and DecodeDate gives that in year,month,day triplet.
once I have that triplet I can "encode_date".
but encode_date, as a function,  it can only receive 1 variable and return a Word (myDate) then:
Code: Pascal  [Select][+][-]
  1. ReadLn (Header.Date);
  2.      myWriteWord (Header.Date);
should I make encode_date a procedure?
Code: Pascal  [Select][+][-]
  1. Uses sysutils;
  2.  
  3. Var YY,MM,DD : Word;
  4.  
  5. Begin
  6.   DecodeDate(Date,YY,MM,DD);
  7.   Writeln (Format ('Today is %d/%d/%d',[dd,mm,yy]));
  8. End.
the way is shown, I dont think in any way I can make a date a single unit.

http://www.freepascal.org/docs-html/rtl/sysutils/date.html

http://www.freepascal.org/docs-html/rtl/sysutils/decodedate.html

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: need help with creating a file like this
« Reply #14 on: October 12, 2015, 05:49:45 pm »
but encode_date, as a function,  it can only receive 1 variable and return a Word (myDate) then:
Code: Pascal  [Select][+][-]
  1. ReadLn (Header.Date);
  2.      myWriteWord (Header.Date);
should I make encode_date a procedure?
I'll show you how I would do it.
Date is an internal function which always returns the current date.
encode_date can be a function but if you always want it to return "Today" you don't need any parameters for it. It only needs to return a Word (encoded).
So:
Code: Pascal  [Select][+][-]
  1. function encode_date: Word;
  2. var  // make certain variables local because you only need them here
  3.   Year, Month, Day: Word;
  4. begin
  5.   //.... the code you already have to determine y_offset, month and day
  6.   result := y_offset shl 9;
  7.   result := result + month shl 5;
  8.   result := result + day;            // <-- these 3 lines set the result of the function
  9. end;
  10.  
  11. begin
  12.   Header.Date := encode_date; // <-- here we call encode_date and set the result into header.date
  13.   myWriteWord(Header.Date);
  14. end;

Do you understand what I did here???


You don't need y_offset, month and day in you main code at all. So there is only need for one result (a Word) from that function encode_date.

 

TinyPortal © 2005-2018