We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,612 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Reading,extracting and saving values in a file as an integer

Working on a program that needs to open a file, read the contents which is a number, save the content as an integer in the program, edit the number by one and then save in the number in the file once more.

2
Contributors
20
Replies
1 Week
Discussion Span
10 Months Ago
Last Updated
21
Views
Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Pascal or Delphi? What do you have so far, and where are you stuck?

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Pascal... Have nothing thus far but this. But it was only created as a test extract.
Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; d:=1; Assign(Counter,'C:\check.txt'); Rewrite(Counter,d); close(counter); d:=d+1;
It all seems basically wrong and am pretty much stuck from the start so all help will do.

Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

What you posted is Delphi code, but those functions still work. The following is a modified example from Delphi's Help to write a value to a text file:

var
  F: TextFile;
  d: Integer;
begin
  d := 1;
  AssignFile(F, 'C:\check.txt');
  Rewrite(F);
  Writeln(F, IntToStr(d));
  CloseFile(F);
end;
pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Now to extract it from the file, put in array then increase the value by one and finally save it back to the file.

Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Try it. You can find lots of examples in the help file.

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

I get run time error(104). Here's code:
`

var User: array of UserRecord;
     var
    F: TextFile;
    d,count: Integer;
   Procedure readcounter;
begin
    AssignFile(F, 'C:\check.txt');
    Rewrite(F);
    while not eof(f) do
    readln(f,d);<----- error pointed to here
    closefile(f);
    end;
    Procedure editcounter;
    begin
    d:=d+1;
    assign(f, 'C:\check.txt');
    rewrite(f);
    writeln(f, IntToStr(d));
//Writeln(F, IntToStr(d));
    CloseFile(F);
    end;
 Procedure checkdata;
 begin
 for count:= 1 to (d) do
 with User[d] do
 if (username = Edit1.text) and (useremail = edit3.text) and (Rname = edit4.text) and (Remail = edit6.text) then
 showmessage('Certain Fields are already taken please re-check and try again')
 else
   username := Edit1.text;
   userpassword := Edit2.text;
   useremail := edit3.text;
   Rname := Edit4.text;
   Rpassword := Edit5.text;
   Remail := Edit6.text;
   application.terminate;
 end;
{ TForm1 }


{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  readcounter;
  checkdata;
  editcounter;
end;

`

Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Rewrite sets the file pointer to the beginning of the file for writing. Since you want to read, use Reset.

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

now its saying raised exception class'External: SIGSEGV'. Then points to line in control.inc
if TMethod(@Self.GetTextBuf).Code = Pointer(@TControl.GetTextBuf)

Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Step through it with the debugger and see where it fails in your code.

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

how does one go about that...

Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Didn't help... Really couldn't understand wat the man was saying at all

Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I'm going through and it points to this in my lpr

`begin<-------- pointed to here
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;

end.`
Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

stepped over it and the assembler popped up with this:
System_FPC_CPUINIT (7)
System_FILLCHAR$formal$LONGINT$BYTE

Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Stepped again and got this:
fpc_initializeunits
and then pointed to this line saying:
0040c7A3 8b1dd0bd5600 mov

Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Step over then it points to line 2 in lpr

Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Set a breakpoint (using F5) on the first line in readcounter and start from there.

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

ok did that, didn't work but i think i've found the main cause of the problem by taking apart and executing each procedure individually. See below:

  Procedure checkdata;
 begin
 for count:= 1 to (d) do
 with User[d] do
 if (username = Edit1.text) and (useremail = edit3.text) and (facebookname = edit4.text) and (facebookemail = edit6.text) then
 begin
 showmessage('Certain Fields are already taken please re-check and try again');
 end
 else
   username := Edit1.text;
   userpassword := Edit2.text;
   useremail := edit3.text;
   facebookname := Edit4.text;
   facebookpassword := Edit5.text;
   facebookemail := Edit6.text;
   application.terminate;     

`
now the block that is causing the problem is

username := Edit1.text;
   userpassword := Edit2.text;
   useremail := edit3.text;
   facebookname := Edit4.text;
   facebookpassword := Edit5.text;
   facebookemail := Edit6.text;
   application.terminate;      
 end;`

Don't know what exactly is the problem, maybe a conversion needs to be done or data types changed or something. Would love to dabble with it now but i have to attend to stuff. But if you do know what to do let me know, Thanks.

Appjacker
Newbie Poster
14 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

for count:= 1 to (d) do with User[d] do

If User is an open array, then the count should be from 0 to d - 1. Apart from that, did you specify a length for the array using SetLength ?

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1375 seconds using 2.75MB