Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article explains the steps required to use the default Photos screensaver to display branding/corporate images
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

792 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