Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

why do i have bug with [Can't open File]

Posted on 2000-02-29
5
Medium Priority
?
300 Views
Last Modified: 2010-04-16
Can you tell em where do i have bug?

Program donors_app;
Uses Crt;

Const
  db_stuff = 'c:\-r76\db_stuff.bin';           {faila atrash. vieta}
  Name_signs = 15;                             {varda garums simbolos}
  SurName_signs = 25;                          {SurVaarda garums simbolos}
  Vec_signs = 3;
  n = 100;                                     {ierakstu skaits (max) }

Type
 donors = record
   Name: string[Name_signs];                   {Vards 15 simb. gars}
   SurName: string[SurName_signs];             {SurVaards 25 simb. gars}
   RegNum: integer;                            {Reg. Numura garums 8 simb.}
   Vecums: String[Vec_signs];                  {Vecums 0 - 150}
   Dzimums: string[4];                            {dzimums - vir./siev.}
End;


FFile = file of donors;                 {faila tips}
rec = array[1..n] of donors;            {masivu tips, pec faila tipa glab.}

var fails: FFile;                       {failam dotais nosaukums}
    kop_rec: integer;                   {kopejo ierakstu skaits}


Function Pareizs_REC(i:integer):boolean;   {parbauda vai eksistee ieraksts}
var g: rec;                                {ierakstu skaits}
    j:integer;                             {skaita ierakstus}
begin
 j:=1;                                     {ierkast nr. 1}
 Pareizs_Rec:= true;                       {ieraksts eksitee}
 {$I-}
 Close(fails); {}                          {aizver failu}
 {$I+}
 Assign(fails,db_stuff);                   {faila/lokacijas asigneshana}
 Reset(fails);                             {izdzeshana}
 While ((Eof(fails))<>true) do
   begin
     read(fails,g[j]);                     {nolasa no faila j-to iekatu}
     inc(j);                               {noapalo j, lai naf bugu}
   end;
 if i+1>j then Pareizs_REC:=false;         {parbauda vai eksiste nakamais ieraksts}
End;

Procedure MakeRec;                             {recordu veidoshana}
var g: rec;
    Y: char;   s,temp: string;   ok: boolean; a,i,b,rez:integer;
      TempSTR,chose: String;  Code,TempG: Integer;
      Label 12; Label 13;
Begin
{$I-}
Close(fails);{}
{$I+}
Assign(fails,db_stuff);
{Reset(fails);}
If IOResult <> 0 then Rewrite(fails); {}
Seek(fails,FileSize(fails));                    {meklee faila beigas}
inc(kop_rec);
ClrScr;
Writeln('Pec neprecizas vertibu ievadishanas, tas bus jaaievada atkartoti!');
GotoXY(3,22);
Write('-press enter-'); Readln;
ClrScr;
Writeln('Ievadiet personas vardu - ',Name_signs,' simboliem');
GotoXY(3,22);
Write('-ievadiet- ');
ReadLn(g[kop_rec].Name);           {ieglabaa masivaa name}
ClrScr;
Writeln('Ievadiet personas uzvardu - ',SurName_signs,' simboliem');
GotoXY(3,22);
Write('-ievadiet- ');
ReadLn(g[kop_rec].SurName);                        {ieglabaa masivaa SurName}

ClrScr;                                            {izveido reg numuru}
Writeln('reg-num tiks noteikts automatiski');
GotoXY(3,22);
Write('-press enter-');
ReadLn;
 Randomize;
    rez := Random(9999);
    if rez<=1000 then rez:=rez+1000;
 ClrScr;
    Write('Jusu registracijas numurs--> ', rez);
    GotoXY(3,22); Write('-press enter-');
    g[kop_rec].RegNum := rez;
readln;

ok:=false;
While ok <> true do                      {ievadam datus -vecums}
begin
13: ClrScr;
    Writeln('Ievadiet personas vecumu: ');
    GotoXY(3,22); Write('-ievadiet- ');
    ReadLn(temp);
      begin
       a:=0;
        for i:=1 to length(temp) do begin
         if (ord(temp[i])>=48) and (ord(temp[i])<=57)
          then b:=1  {ASCII kodaa sk}
        else a:=a+1;
     if a>=1 then goto 13
       else ok:=true;
       g[kop_rec].vecums:=temp;
  end;
 end;
end;

Repeat                       {parbauda ievadisha par siev/vir}
   ClrScr;
    Writeln('Izvelas vajadzigo: [0-Siev/1-Vir]');
    GotoXY(3,22); Write('-ievadiet- ');
      ReadLn(TempSTR);
      Val(TempSTR,TempG,Code);
    Until ((Code = 0) And (TempG In [0..1]));
       if tempStr = '1' then chose:='vir.';
       if tempStr = '0' then chose:='siev.';
      g[kop_rec].Dzimums:=chose;
ClrScr;
Writeln('Saglabat datus failaa?  [Y/N]');
GotoXY(3,22); Write('-izvele- ');
readln(Y);
if (Y = 'Y') or (Y = 'y') then write(fails,g[kop_rec]);
End;

Procedure PrintRec;
var g: rec;
i,j: integer;
Begin
ClrScr;
{$I-}
Close(fails);{}
{$I+}
Assign(fails,db_stuff);
Reset(fails);{}
i:=1; j:=0;
While ((Eof(fails))<>true) do
 begin
   inc(j);
   Read(fails,g[i]); {}
   Writeln('nr:             ',i);
   Writeln('name:           ', g[i].name);
   Writeln('surname:        ', g[i].surname);
   Writeln('reg nr:         ', g[i].regnum);
   Writeln('vecums:         ', g[i].vecums);
   Writeln('dzimums:        ', g[i].dzimums);
   writeln;
 end;
readln;
End;


begin
MakeRec;
PrintRec;
end.


0
Comment
Question by:fr0x
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Hypo
ID: 2569752
I can run your code without any errors. My guess is either that the file c:\-r76\db_stuff.bin doesn't exist or that you don't have access to write to the disk. e.g. if you're on a network, you might not have access to write files on C:

other than that, I don't know why your code throws an error at you!
0
 

Author Comment

by:fr0x
ID: 2569917
Hmz.. I forget to told, taht i have bug with ..!!

I can add data and print data on screen! but when i start this process again.. to add new data to file *.bin!

all data is been erased and i can save just the last data entry!

why?

i had to get such a result:
Number 1.
Name sjkhdj
surname lkss
....

Number 2
Name jdewh
Surname kjxhid
....

Number 3
Name jdesdesd
Surname kjsdfsfe
....

etc.

Can you tell me where is a problem?
0
 
LVL 3

Accepted Solution

by:
sumant032199 earned 150 total points
ID: 2569942
hello fr0x,
There can be following reasons

1. File does not exist.
2. PAth does not exist.
3. Drive does not exist.
4. File readonly.
5. Shared file under multiuser / multitasking system.
0
 

Author Comment

by:fr0x
ID: 2570009
I have this error:
1. File does not exist.

cn you tell how to solve this?
0
 
LVL 12

Expert Comment

by:Hypo
ID: 2570611
Ok, I've made a few changes to your code... At two points you started the procedures (Make_Rec and Print_Rec) by closing a file (fails), I moved those line to the end of the procedures instead. And I think it works fine now!
(I would have posted this as an answer if I it were'nt already locked!)


Take a look at it, Does it work like you wanted it to do?

Program donors_app;
Uses Crt;

Const
  db_stuff = 'e:\program\pascal\test2.pas';           {faila atrash. vieta}
  Name_signs = 15;                             {varda garums simbolos}
  SurName_signs = 25;                          {SurVaarda garums simbolos}
  Vec_signs = 3;
  n = 100;                                     {ierakstu skaits (max) }

Type
 donors = record
   Name: string[Name_signs];                   {Vards 15 simb. gars}
   SurName: string[SurName_signs];             {SurVaards 25 simb. gars}
   RegNum: integer;                            {Reg. Numura garums 8 simb.}
   Vecums: String[Vec_signs];                  {Vecums 0 - 150}
   Dzimums: string[4];                            {dzimums - vir./siev.}
End;


FFile = file of donors;                 {faila tips}
rec = array[1..n] of donors;            {masivu tips, pec faila tipa glab.}

var fails: FFile;                       {failam dotais nosaukums}
    kop_rec: integer;                   {kopejo ierakstu skaits}


Function Pareizs_REC(i:integer):boolean;   {parbauda vai eksistee ieraksts}
var g: rec;                                {ierakstu skaits}
    j:integer;                             {skaita ierakstus}
begin
 j:=1;                                     {ierkast nr. 1}
 Pareizs_Rec:= true;                       {ieraksts eksitee}
 {$I-}
 Close(fails); {}                          {aizver failu}
 {$I+}
 Assign(fails,db_stuff);                   {faila/lokacijas asigneshana}
 Reset(fails);                             {izdzeshana}
 While ((Eof(fails))<>true) do
   begin
     read(fails,g[j]);                     {nolasa no faila j-to iekatu}
     inc(j);                               {noapalo j, lai naf bugu}
   end;
 if i+1>j then Pareizs_REC:=false;         {parbauda vai eksiste nakamais ieraksts}
End;

Procedure MakeRec;                             {recordu veidoshana}
var g: rec;
    Y: char;   s,temp: string;   ok: boolean; a,i,b,rez:integer;
      TempSTR,chose: String;  Code,TempG: Integer;
      Label 12; Label 13;
Begin
{$I-}
Assign(fails,db_stuff);
Reset(fails);
If IOResult <> 0 then Rewrite(fails); {}
Seek(fails,FileSize(fails));                    {meklee faila beigas}
inc(kop_rec);
ClrScr;
Writeln('Pec neprecizas vertibu ievadishanas, tas bus jaaievada atkartoti!');
GotoXY(3,22);
Write('-press enter-'); Readln;
ClrScr;
Writeln('Ievadiet personas vardu - ',Name_signs,' simboliem');
GotoXY(3,22);
Write('-ievadiet- ');
ReadLn(g[kop_rec].Name);           {ieglabaa masivaa name}
ClrScr;
Writeln('Ievadiet personas uzvardu - ',SurName_signs,' simboliem');
GotoXY(3,22);
Write('-ievadiet- ');
ReadLn(g[kop_rec].SurName);                        {ieglabaa masivaa SurName}

ClrScr;                                            {izveido reg numuru}
Writeln('reg-num tiks noteikts automatiski');
GotoXY(3,22);
Write('-press enter-');
ReadLn;
 Randomize;
    rez := Random(9999);
    if rez<=1000 then rez:=rez+1000;
 ClrScr;
    Write('Jusu registracijas numurs--> ', rez);
    GotoXY(3,22); Write('-press enter-');
    g[kop_rec].RegNum := rez;
readln;

ok:=false;
While ok <> true do                      {ievadam datus -vecums}
begin
13: ClrScr;
    Writeln('Ievadiet personas vecumu: ');
    GotoXY(3,22); Write('-ievadiet- ');
    ReadLn(temp);
      begin
       a:=0;
        for i:=1 to length(temp) do begin
         if (ord(temp[i])>=48) and (ord(temp[i])<=57)
          then b:=1  {ASCII kodaa sk}
        else a:=a+1;
     if a>=1 then goto 13
       else ok:=true;
       g[kop_rec].vecums:=temp;
  end;
 end;
end;

Repeat                       {parbauda ievadisha par siev/vir}
   ClrScr;
    Writeln('Izvelas vajadzigo: [0-Siev/1-Vir]');
    GotoXY(3,22); Write('-ievadiet- ');
      ReadLn(TempSTR);
      Val(TempSTR,TempG,Code);
    Until ((Code = 0) And (TempG In [0..1]));
       if tempStr = '1' then chose:='vir.';
       if tempStr = '0' then chose:='siev.';
      g[kop_rec].Dzimums:=chose;
ClrScr;
Writeln('Saglabat datus failaa?  [Y/N]');
GotoXY(3,22); Write('-izvele- ');
readln(Y);
if (Y = 'Y') or (Y = 'y') then write(fails,g[kop_rec]);
close(fails);
{$I+}
End;

Procedure PrintRec;
var g: rec;
i,j: integer;
Begin
ClrScr;
{$I-}
Assign(fails,db_stuff);
Reset(fails);{}
i:=1; j:=0;
While ((Eof(fails))<>true) do
 begin
   inc(j);
   Read(fails,g[i]); {}
   Writeln('nr:             ',i);
   Writeln('name:           ', g[i].name);
   Writeln('surname:        ', g[i].surname);
   Writeln('reg nr:         ', g[i].regnum);
   Writeln('vecums:         ', g[i].vecums);
   Writeln('dzimums:        ', g[i].dzimums);
   writeln;
 end;
readln;
Close(fails);{}
{$I+}
End;


begin
MakeRec;
PrintRec;
end.

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Do online retailers and e-commerce sites even need to bother with the season? Is it lucrative to update and prepare your site for Black Friday and Cyber Monday shoppers? We'll find out now.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question