Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Adding record to a file, help

record file delphi help

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1 delphiuser

delphiuser

    CC Newcomer

  • Member
  • PipPip
  • 23 posts

Posted 20 December 2012 - 03:55 AM

i made a file for items and now i want to add items into the file using item id, but every time i add an item it keeps overwritting the first record.
below is my code.


procedure Tfrmadditem2.btnaddClick(Sender: TObject);
var
itemtemp,itemfile:Titemfile;   declarations as with personfile
newitemrec, itemrec,item:Titem;
itemID :integer;

  addrecord : boolean;
  count : integer;
  recordchanged : boolean;
  record_added : boolean;
inserted:Boolean;
begin
;

assignfile (itemfile, 'Itemfile.dat');
		  reset    (itemfile);
		  assignfile (itemtemp, 'Tempitemfile.dat');
		  rewrite    (itemtemp);
  addrecord := false;
  while not eof(itemfile) do begin
  read(itemfile, item);
  if (item.Item_type = edttype.Text) and (item.Item_Company = edtcomp.Text)
							  and (item.Item_Name= edtname.Text) then
  addrecord := true;
  end;
   assignfile (itemfile, 'Itemfile.dat');
		  reset    (itemfile);
  assignfile(itemtemp,'Tempitemfile.dat');
  rewrite (itemtemp);

  if addrecord = False then begin
  count:=1;
    while not eof(itemfile) do begin
    read(itemfile, item);
		  count := item.Item_ID + 1 ;
    write(itemtemp, item);
    end;
  if error_no = false then begin
  with item do begin
    Item_type := edttype.Text;
    Item_Company :=edtcomp.text;
    Item_Name := edtname.Text;
    Item_Quantity := strtoint(edtquantity.Text);
    Item_Price := strtocurr(edtprice.Text);
    Item_ID := count;
    EdtID.text := inttostr(item.Item_ID);
    write(itemtemp, item);
    showmessage('item Added');
  end;
  end;
closefile(itemfile);
closefile(itemtemp);
erase(itemfile);
rename(itemtemp,'Itemfile.dat');
end else begin showmessage('itemalready exists on system.'); closefile(itemfile) end;
end;


#2 Roman Y

Roman Y

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 196 posts

Posted 20 December 2012 - 04:16 AM

I might be way off here (have never done any pascal nor do I know why I comment on this :) ) But when you open a file in rewrite mode it will erase the previous content so try opening itemtemp (the second time after you've read in all the previous data ) with append (itemtemp); and see if it works.

Edited by Roman Y, 20 December 2012 - 04:17 AM.


#3 delphiuser

delphiuser

    CC Newcomer

  • Member
  • PipPip
  • 23 posts

Posted 20 December 2012 - 04:25 AM

I might be way off here (have never done any pascal nor do I know why I comment on this :) ) But when you open a file in rewrite mode it will erase the previous content so try opening itemtemp (the second time after you've read in all the previous data ) with append (itemtemp); and see if it works.



i get an error saying incompatitable types

#4 Roman Y

Roman Y

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 196 posts

Posted 20 December 2012 - 04:42 AM

Oh.. right.. just read that append only works with text files...

Then try navigating to the last element of the file before writing to it. seek(itemtemp, filesize(itemtemp));

#5 delphiuser

delphiuser

    CC Newcomer

  • Member
  • PipPip
  • 23 posts

Posted 20 December 2012 - 05:21 AM

I dont know what im doing wrong, where in the code should i add it .

#6 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts

Posted 21 December 2012 - 09:56 AM

I assume that this is for exercise. So I will not comment on the design.

Like already mentioned by Roman Y (and also actually solved), your main problem was caused by calling Rewrite on existing file. It will delete existing file and create new one. The solution is to call Rewrite only if the file does not exist, and otherwise call Reset. When you use Reset, you need to move the file pointer to the end of the file. You can use code contributed by Roman Y or you can do "dummy" read until Eof becomes true (like you did with itemfile).

So, instead of simple rewrite (itemtemp); you should do:

  try
    Reset(itemtemp);
    Seek(itemtemp, FileSize(itemtemp));
  except
    on E:EInOutError do
      rewrite(itemTemp);
  end;  

Note that I used try-except block here because when you call Reset on non-existing file, it will raise <em>EInOutError,/em> exception. That would make the code jump onto the Rewrite call.


Another problem was that you did AssignFile on already opened file variable. Note the third and fourth calls on AssignFile? They were assigning files to itemfile and itemtemp which already opened by prior AssignFile followed by Reset (in case of itemfile) and Rewrite (in case of itemtemp).

I don't exactly recall the error this would caused. But it is safer to avoid this in the future. Always use AssignFile on closed or uninitiated file variable.

Edited by LuthfiHakim, 22 December 2012 - 09:44 AM.


#7 delphiuser

delphiuser

    CC Newcomer

  • Member
  • PipPip
  • 23 posts

Posted 22 December 2012 - 04:30 AM

Thanks again, It did work , i replaced it with the incorrect line first time round.

#8 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts

Posted 22 December 2012 - 09:52 AM

That's great!

#9 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts

Posted 22 December 2012 - 11:38 AM

This topic has been marked as SOLVED. If you have a similar question or topic, you can go back to the subforum and start a new topic to continue discussions.

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download