Solved

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

Posted on 2000-02-29
5
294 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 50 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

For months I had no idea how to 'discover' the IP address of the other end of a link (without asking someone who knows), and it drove me batty. Think about it. You can't use Cisco Discovery Protocol (CDP) because it's not implemented on the ASAs.…
Data breaches are on the rise, and companies are preparing by boosting their cybersecurity budgets. According to the Cybersecurity Market Report (http://www.cybersecurityventures.com/cybersecurity-market-report), worldwide spending on cybersecurity …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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